Java Reference
In-Depth Information
java has a special reference type that is called null type. the null type does not have a name. the null type has a
literal value, which is represented by null . the null type is assignment compatible with all other reference types. You can
assign any reference type variable a null value. You can cast null value to any reference type. It is to be emphasized
that null is a literal value of “null reference type,” not a keyword.
Tip
Using Dot Notation to Access Fields of a Class
Dot notation is used to refer to instance variables. The general form of the dot notation syntax is
<<Reference Variable Name>>.<<Instance Variable Name>>
For example, you use jack.name to refer to the name instance variable of the instance to which the jack reference
variable is referring. If you want to assign a value to the name instance variable, you can use
jack.name = "Jack Parker";
The following statement assigns the value of the name instance variable to a String variable aName :
String aName = jack.name;
How do you refer to class variables? You have two ways to refer to a class variable using dot notation.
You can refer to a class variable using the name of the class.
<<Class Name>>.<<Class Variable Name>>
For example, you can use Human.count to refer to the count class variable of the Human class.
To assign a new value, say 101, to the count class variable you can write
Human.count = 101;
To read the value of the count class variable into a variable called population , you can use
long population = Human.count;
You can also use a reference variable to refer to the class variable of a class. For example, you
can use jack.count to refer to the count class variable of the Human class. You can use the
following statement to assign value, say 101, to the count class variable:
jack.count = 101;
The following statement reads the value of the count class variable into a variable called
population :
long population = jack.count;
Both of the above statements assume that jack is a reference variable of Human type and it
refers to a valid Human instance.
 
 
Search WWH ::




Custom Search