Declaring Object References, Instantiating Objects and Initializing Object References

Object reference variables are variables containing an address to an Object in memory.

To declare, instantiate and initialise an Object Reference Variable:
  • Declare - Specifies its identifier and the Object Type of the referenced class
  • Instantiate - Using the new keyword
  • Initialise - Assigns the Object to the Object Reference Variable
Declaring Object Reference Variables:
Syntax:
Classname identifier;
where
Classname is the Class or the type of Object referred to with the Object Reference
identifier is the variable name of type Classname

Instantiating an Object:
Syntax:
new Classname();

where
new keyword is used to create an Object Instance from a Class
Classname is the Class or Type of Object being created

Initializing Object Reference Variables:
Syntax:
identifier = new Classname();

Using an Object Reference Variable to Manipulate Data:
The dot (.) operator is used with the Object Reference to manipulate the values or to activate the Object methods

Storing Object Reference Variables in Memory:
Object Reference Variables hold the memory address of Objects in memory. Memory addresses are usually written in hexadecimal notation such as 0x332006. Local Object Reference Variables and their values are stored in Stack Memory while the Objects that they reference to are stored in Heap Memory.

Assigning a Reference From One Variable to Another:
Upon execution of the above codes, both myShoe and yourShoe reference variables contain the same address to the same object. Unless another reference variable was pointing myShoe object, it would be garbage collected.