Sunday, December 15, 2013

[Java] How do you declare an object’s reference variable?


Objects are accessed via objects' reference variables,which contain references to the objects. The syntax to declare a reference variable for an object is

ClassName objectRefVar;
 A class is a reference type, which means that a variable of the class type can reference an unstance of the class. The variable myCircle can reference a Circle object. The next statement creates an object and assigns its reference to myCircle:
   myCircle = new Circle();
You can write a single statement that combines the declaration of an object reference variable,the creation of an object, and the assigning of an object reference to the variable with the following syntax:
    ClassName objectRefVar = new ClassName();
Here is an example:
    Circle myCircle = new Circle();
The variable myCircle holds a reference to a Circle object.

Remarks
Text provided above does not means is the model answer, and was posted here for an reference to someone who interested the titled topic only, if there is something incorrect, please leave a comment so that i can correct it, many thanks!

Reference:
Introduction to Java programming, Y. Daniel Liang, 10th edition (P.333)

No comments :

Post a Comment