Java Reference
In-Depth Information
p.x = 3;
p.y = 3;
p.useCount++;
p.origin.useCount++;
System.out.println("(" + q.x + "," + q.y + ")");
System.out.println(q.useCount);
System.out.println(q.origin == Point.origin);
System.out.println(q.origin.useCount);
}
}
This program prints:
(2,2)
0
true
1
showing that changing the fields x , y , and useCount of p does not affect the fields of
q , because these fields are instance variables in distinct objects. In this example, the
class variable origin of the class Point is referenced both using the class name as a qual-
ifier, in Point.origin , and using variables of the class type in field access expressions
15.11 ) , as in p.origin and q.origin . These two ways of accessing the origin class variable
access the same object, evidenced by the fact that the value of the reference equality
expression (§ 15.21.3 ) :
q.origin==Point.origin
is true. Further evidence is that the incrementation:
p.origin.useCount++;
causes the value of q.origin.useCount to be 1 ; this is so because p.origin and q.origin refer
to the same variable.
Example 8.3.1.1-2. Hiding of Class Variables
Click here to view code image
class Point {
static int x = 2;
}
class Test extends Point {
static double x = 4.7;
public static void main(String[] args) {
new Test().printX();
Search WWH ::




Custom Search