New Keyword in Java

The new keyword is a reserved keyword in Java programming which is used to create a new object or instance of a class. It allocates memory on the heap for the object and returns a reference to that memory.

The reference returned by the new operator represents the memory address of the created object in the heap. This reference is stored in a stack memory which is represented by a variable which is called an object reference variable. This variable is used to access the fields (i.e. variables) and methods of the object.

The basic syntax to use new keyword in the object creation is as follows:

ClassName objectName = new ClassName();

In the above syntax of object creation, “ClassName” represents the name of class for which an object is being created. “objectName” is the reference variable pointing to the memory location where the object is stored.

Main Functions of the new Keyword in Java

There are four main functions of new keyword in Java programming. They are as:

  • Object Creation: The main function of the new keyword is to create objects of a class.
  • Memory Allocation: After creating an object, it allocates the required amount of memory for that object on the heap.
  • Constructor Invocation: When you create an object of a class, the new keyword calls the constructor of the class to initialize the object.
  • Reference Return: It returns a reference (memory address) pointing to the object in the heap, which can be used to access members and methods of a class.

Examples of Using new Keyword

Example 1: Using an object creation

package myProgram;
public class Person {
// Declare instance variables.	
   String name;
   int age;
    
// Declare a two parameterized constructor.
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
// Declare an instance method.
   public void displayInfo() {
      System.out.println("Name: " + name + ", Age: " + age);
   }
// Main method.
   public static void main(String[] args) {
   // Creating an object of class Person using new keyword.	
      Person person = new Person("Mark", 30);
   // Calling method using object reference variable.    
      person.displayInfo();
    }
}
Output:
    Name: Mark, Age: 30

In this example, the new keyword creates an instance or object of the Person class. It invokes the constructor Person(String name, int age) to initialize the object’s properties. Then, we have called displayInfo() method using object reference variable.

2. Allocating Memory for Arrays: The new keyword is also used to allocate memory for arrays in Java like this:

int[] numbers = new int[5];

Here, an integer array of size 5 is created using the new keyword. Without using new keyword, it is not possible to dynamically create arrays.

3. Anonymous Objects: Sometimes, we create an object of a class without assigning it to a reference variable. This is called an anonymous object. We can create an anonymous object like this:

new Person("Mark", 25).displayInfo();

    However, you cannot reuse this object. It is a useful technique when you need to create only one object of the class.

    In this tutorial, you have learned about new keyword in Java programming with some examples. I hope that you will have understood the basic concepts of using new keyword in object creation, constructor call, and memory allocation.

    Please Share Your Love.

    Leave a Reply

    Your email address will not be published. Required fields are marked *