Saturday, November 20, 2021

Object Creation Using new Operator in Java

In OOP a class provides the blueprint for objects; you create an object from a class. When a new class is created essentially a new data type is created. This type can be used to declare objects of that type. In this post we'll see what all happens when an object is created using new operator in Java.

Creating Objects in Java

Creation of an object for a class in Java is done in three steps-

  • Declaration- Declare a variable of the class type, note that no object is defined yet. This variable can refer to an object as and when the object is created.
  • Instantiation- Create an object and assign it to the variable created in step 1. This object creation is done using new operator.
  • Initialization- Class name followed by parenthesis after the new operator (new classname()) means calling the constructor of the class to initialize the object.

General form of using new operator by combining all these three steps is as-

Class_Name class_Variable = new Class_Name();

Here Class_Name is the class whose object is created and class_Variable is the variable that refers to the created object. Here note that when you refer Class_Name followed by parenthesis, you are actually calling the constructor of the class to initialize the object. That constructor may be a constructor with arguments or a no-argument constructor (or default constructor).

Let’s go through the steps of declaration, instantiation and initialization-

Declaring a Variable to Refer to an Object

When you just declare a variable no memory is allocated for it. As example if you declare a variable obj of class Test-

Test obj;

What you have done here is to notify the Java compiler that you will use obj to refer to data whose type is Test.

At this stage, when you have just declared the variable obj, it doesn’t refer to an object. Actually its value is undetermined at this time. To create an object you need to use the new operator in Java. You must assign an object to obj variable before you use it in your code. Otherwise, you will get a compiler error.

A variable in this state, which currently has no reference to any object can be pictorially represented as follows-

object declaration in Java
Object declaration

Instantiating a Java Class

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

The new operator requires a single, postfix argument: a call to a constructor and it returns the refrence to the object it created. This reference is usually assigned to a variable of the appropriate type, As example

Test obj = new Test(9, 10);

Initializing an Object in Java

The last step is to initialize an object. This is the process where constructor of the class is called to initialize the object.

As example-

If we have a Test class as follows-

public class Test{
  public int x = 0;
  public int y = 0;
  public Test(int x, int y) {
    this.x = x;
    this.y = y;

This class contains a single constructor which takes two integer arguments, in order to initialize a object created using new operator you can have a statement as follows-

Test obj = new Test(9, 10);

Here you have a variable obj of type Test, using new operator you have created a new object of class Test and initialized it to have values 9 and 10 for variables x and y respectively. So, this instantiation of class Test has its variables initialized to 9 and 10 i.e. obj.x = 9 and obj.y = 10.

obj is the variable that refers to the object.

If we have to pictorially represent this instantiation and initialization, it will look as follows.

Initializing an Object in Java
Initializing an Object

That's all for this topic Object Creation Using new Operator in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page

Related Topics

  1. Package in Java
  2. Access Modifiers in Java - Public, Private, Protected and Default
  3. Abstraction in Java
  4. Difference Between Abstract Class And Interface in Java
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Association, Aggregation and Composition in Java
  2. Initializer block in Java
  3. String in Java Tutorial
  4. Nested Try Statements in Java Exception Handling
  5. Generics in Java
  6. Map operation in Java Stream API
  7. How HashMap Works Internally in Java
  8. How ArrayList Works Internally in Java

1 comment: