CLASSES
Field Modifiers
8.3.1
appear in a field declaration, it is customary, though not required, that they appear
in the order consistent with that shown above in the production for
FieldModifier
.
8.3.1.1
static
Fields
If a field is declared
static
, there exists exactly one incarnation of the field, no
matter how many instances (possibly zero) of the class may eventually be created.
A
static
 field, sometimes called a
class variable
, is incarnated when the class is
initialized ( 12.4).
A field that is not declared
static
 (sometimes called a non 
static
 field) is
called an
instance variable
. Whenever a new instance of a class is created, a new
variable associated with that instance is created for every instance variable
declared in that class or any of its superclasses.
The example program:
class Point {
int x, y, useCount;
Point(int x, int y) { this.x = x; this.y = y; }
final static Point origin = new Point(0, 0);
}
class Test {
public static void main(String[] args) {
Point p = new Point(1,1);
Point q = new Point(2,2);
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);
}
}
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 qualifier, in
Point.origin
, and using variables of the class type in
field access expressions ( 15.10), 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.20.3):
145






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved