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 carOr combined if they belong to the same appropriate class, separated by a comma:
Car myCar;
// declaring my father's car
Car myFatherCar ;
//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:
how to compare colors(fields value) of two car class objects.
thanks!!!!!!!
it is very nice & easy to understand.
it's easily understand the concept....
thanks so much...& clearly explain the example...thanks
it's easily understand the concept....
thanks so much...& clearly explain the example...thanks
Very nice and simple.....daniel frm nigeria
what is meaning of initialization of object??? is it initialized by zero through constructer???
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.
please explain in very complex way.if we follow this our mentors will say
"not correct"
really helpfull thanxxx a lot
It is easy to understand Very Nice
Thanks alot
this instantiation definition actually solved my question. thank you for sharing this post.
it was clear and precise
More infotech posts
Post a Comment