New Keyword in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The new keyword in Java instantiates a class by allocating desired memory for an associated new object. It then returns a reference to that memory. Many times, the new keyword in Java is also used to create the array object. The new keyword is followed by a call to a constructor, which instantiates the new object.

Introduction

We use the new keyword in Java to instantiate a class via allocating required memory for a new object. The new keyword returns a reference to that memory post object creation. Sometimes, we can make use of the new keyword in Java to create the array objects.

Syntax:

Important points about the new keyword:

  • new keyword allocates memory to the new objects at runtime.
  • Memory is allocated to the new objects from the heap memory.
  • The new keyword is followed by a call to a constructor, which instantiates the new object.

Process of Object Creation

The process of creating an instance of a class has to follow the following steps given below:

  • Declaration
  • Instantiation
  • Initialization

Now let us understand them one by one.

Declaration

In order to create an object, we start by declaring a variable of that class type. This variable can be used to refer to that newly created object.

Following is the syntax for declaration, along with an example:

Syntax:

Example:

The defined variable in the state currently references no object and is just understood as the variable name, myBox of data-type Box.

declaring reference to an object of a class Box

Instantiation

After variable declaration, we need to acquire an actual, physical copy of that object (memory reference) and assign it to the variable. The new keyword instantiates a class by allocating memory for a new object and returning a reference to that memory. This dynamic memory allocation happens at the run-time.

Note:

The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

The new operator requires exactly one postfix parameter: a call to the class constructor. A constructor defines what occurs when an object of a class is created. Constructors are an important part of all classes and have many significant attributes. In the below example, we will use the default constructor.

The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:

Syntax:

Example:

Now let us concentrate on the class Box prototype before jumping on dynamic allocation of memory.

In the diagram below:

  • We are declaring a variable myBox of data type Box. We have not yet assigned a memory reference to it.
  • In the second statement, the new keyword instantiates the class by allocating memory to its new object and returns a reference to that memory to the myBox variable, thereby initializing it.

Instantiation

Key Points:

  • The key phrase “instantiating a class” can be described as “creating an object.” When we create an object, we are actually creating an “instance” of the class.
  • The memory reference returned by the new operator can be directly used in an expression, as shown below. It doesn't need to be assigned to a variable of appropriate data type.
  • Since arrays are objects in Java, hence while instantiating arrays, we use the new operator. For example:
  • We need to know that Java’s primitive types are never implemented as objects and are rather implemented as “normal” variables, as this can lead to good efficiency.

Initialization

Below we have covered a few examples that can help us give insight with respect to the initialization of variables using the new keyword in Java.

Using Default Constructor

All classes have at least one constructor. If a class does not explicitly declare any, the online Java compiler automatically provides a no-argument constructor, called the default constructor.

Let us understand with the below code how we can initialize a variable using the default constructor of a class and the new keyword.

Code:

Output:

Explanation:

  • The new keyword instantiates the class and returns the memory reference of the new object to initialize the variable obj.
  • Using the obj variable, we can access the members of the new object as shown in the output.

Using Parameterized Constructor

Let us see how we can initialize a variable using the parameterized constructor of a class and the new keyword.

Code:

Output:

Explanation:

  • The new keyword instantiates the class using the parameterized constructor and returns the memory reference of the new object to initialize the variable obj.
  • Using the obj variable, we can access the members of the new object as shown in the output.

Create an Array Object

Let us understand with the below code how we can implement the new keyword in Java and create an array object using the new keyword.

Code:

Output:

Explanation:

  • In the code above, we are using the new keyword to create the array object of size five in the heap memory.
  • The memory reference is returned by the new keyword and is stored in the arr variable.

Use New Keyword in Java Collections

Let us see how we can initialize variables using the new keyword in Java Collections.

Code:

Output:

Explanation:

  • In the code above, we are using the new keyword to create an object of the List class in the heap memory.
  • The memory reference is returned by the new keyword, and it is used to initialize the obj variable.

Conclusion

  • The new keyword in Java instantiates a class by allocating memory for an associated new object. It then returns a reference for that memory.
  • This new keyword in Java allocates the memory at the runtime itself.
  • The new keyword allocates memory to the objects in the heap memory.
  • The new keyword requires a single postfix constructor to instantiate the corresponding class.
  • The key phrase “instantiating a class” can be described as “creating an object.” When we create an object, we are actually creating an “instance” of the class.
  • Java primitive types are never implemented as objects and are rather implemented as “normal” variables, as this can lead to good efficiency. We can refer to Wrapper Classes for understanding the primitive data types.
  • We can initialize a variable using the memory reference returned by the new keyword.
  • The memory reference can be used directly to call the members of the new object without assigning it to a variable of the appropriate type.