Wednesday, 22 February 2017

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.

Creating Objects

Creation of an object for a class 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 compiler that you will use obj to refer to data whose type is Test.

At this stage, when you have just declared the variable test, 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. You must assign an object to obj 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
Object declaration

Instantiating a 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

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;
    //constructor
    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
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!


Related Topics

  1. Package in Java
  2. Access modifiers in Java
  3. Abstraction in Java
  4. Difference between abstract class and interface
  5. abstract class in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment