Difference between Declaration, Instantiation and Initialization of the objects in Java

Most of the time Java beginners get confused between the terminology "object","reference","instance". Also, there's a  misunderstanding between Declaration of object, instantiation of the object and initialization of objects. Find out the difference between three of them.

Before we continue remember one most important line "A reference refers (points) to an instance of an object."

1. Declaration of objects:

Declaration of objects in Java is similar as we declare variables of built in data types that is,
data_type   variable_name;

For example,
int  number;

Same way we can declare an object variable that refers to type “Class”. It takes the following syntax,
Class_name   object_variable;

For example,
Person  p;

will declare object variable “p” that refers to type “Person” in the memory heap.(Here we can consider our class to be a user defined data-type.)

If you declare “p” like this, its value will be undetermined until an object is actually created and assigned to it.

Simply declaring a reference variable does not create an object. For that, you need to use the new operator, as described in the next section.

You must assign an object to “p” before you use it in your code. Otherwise, you will get a compiler error.


2. Instantiation of Objects:

Once we have declared the object variable we need to instantiate it with an object of the corresponding data-type i.e. 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.

In short, Instantiating the object means nothing but just allocating the heap memory of the declared object variable of class type.

For above example we can instantiate the object variable “p” like this,
p = new Person();

We can also declare and instantiate the object by following,
Person p = new Person();


3. Initialization of the objects:

The new operator is followed by a call to a constructor, which actually initializes the new object.

Thus, in above statement “ Person(); ” will initialize the actual object and will recognize the appropriate constructor.

Initialization concept can be more precisely understood when we have multiple constructor for the same class. For example,

public class Person {

int age = 20 ;

String name = "Mr. Lazy";

Person(){
System.out.println("Default constructor called.");
}


Person(int argument1){
System.out.println("Constructor with one argument called.");
}


Person(int argument1,int argument2){
System.out.println("Constructor with two arguments called.");
}


public static void main(String args[]){

Person p1 = new Person(); // this will invoke default constructor.

Person p2 = new Person(10); // this will invoke constructor with one argument

Person p3 = new Person(10,20);// this will invoke constructor with two arguments.

}

}

As we can see in this example the Java compiler differentiates the constructors based on the number and the type of the arguments. Thus, the object is initialized based on the arguments provided with it.

Happy coding! :)

1 comment:

  1. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing morejava training in chennai

    ReplyDelete