Javatpoint Logo
Javatpoint Logo

Java new Keyword

The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.

Syntax

Points to remember

  • It is used to create the object.
  • It allocates the memory at runtime.
  • All objects occupy memory in the heap area.
  • It invokes the object constructor.
  • It requires a single, postfix argument to call the constructor

Examples of Java new Keyword

Example 1

Let's see a simple example to create an object using new keyword and invoking the method using the corresponding object reference.

Output:

Invoking Method

Example 2

Let's see a simple example to create an object using new keyword and invoking the constructor using the corresponding object reference.

Output:

Invoking Constructor

Example 3

Here, we create an object using new keyword and invoke the parameterized constructor.

Output:

30

Example 4

Let's see an example to create an array object using the new keyword.

Output:

Array length: 3

Example 5

Let's see an example to use new keywords in Java collections.

Output:

[Java, C++, Python]
Next TopicJava Keywords





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA