Java Class Instantiation

The process of creating objects from a class is called instantiation. So an object is always an instance of a class which represents the blueprint.
The object is constructed using the class and must be created before being used in a program.
Objects are manipulated through object references (also called reference values or simply references)

Creating objects in Java usually follow these steps:

1- Declaration of a reference variable of the appropriate class which will store a reference to the object.
For example:

//declaring my car
Car myCar;

// declaring my father's car
Car myFatherCar ;
Or combined if they belong to the same appropriate class, separated by a comma:

//declaring my car and my father's car
Car myCar, myFatherCar ;

2- Creating an object
This involves using the new operator and calling a constructor to create an instance of the class.
The new operator returns a reference to a new instance of the Car class.
The reference can be assigned to a reference variable of the appropriate class, here: myCar and myFatherCar.

// instantiating myCar from the class Car,
//having the String "black" as parameter value.
myCar = new Car("black");

// instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
myFatherCar = new Car("blue");

Each object has a unique identity and has its own copy of the fields declared in the class.

The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.

The declaration and initialization can be combined:

// declaring and instantiating myCar from the class Car,
//having the String "black" as parameter value.
Car myCar = new Car("black");

// declaring and instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
Car myFatherCar = new Car("blue");

11 comments:

Unknown said...

how to compare colors(fields value) of two car class objects.

Anonymous said...

thanks!!!!!!!
it is very nice & easy to understand.

Anonymous said...

it's easily understand the concept....
thanks so much...& clearly explain the example...thanks

mahesh said...

it's easily understand the concept....
thanks so much...& clearly explain the example...thanks

Anonymous said...

Very nice and simple.....daniel frm nigeria

Anonymous said...

what is meaning of initialization of object??? is it initialized by zero through constructer???

Anonymous said...

Thanks for this post! I couldn't make sense of the repeated class name whenever (compact) examples showed how to instantiate an object (eg. "Car whateverName" vs. "new Car()", but I totally get it now that you showed the two processes separately.

Anonymous said...

please explain in very complex way.if we follow this our mentors will say
"not correct"

Unknown said...

really helpfull thanxxx a lot

Unknown said...

It is easy to understand Very Nice
Thanks alot

chetotrix said...

this instantiation definition actually solved my question. thank you for sharing this post.
it was clear and precise
More infotech posts