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");