Java Reference
In-Depth Information
FieldModifier: one of
Annotation
public protected private
static final transient volatile
and
T
has a (meta-)annotation
m
that corresponds to
java.lang.annotation.Target
, then
m
must
have an element whose value is
java.lang.annotation.ElementType.FIELD
, or a compile-time er-
ror occurs.
It is a compile-time error if the same modifier appears more than once in a field declaration,
or if a field declaration has more than one of the access modifiers
public
,
protected
, and
private
.
If two or more (distinct) field modifiers 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
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 (§
12.5
), a new variable associated
with that instance is created for every instance variable declared in that class or any of its
superclasses.
Example 8.3.1.1-1.
static
Fields
class Point {
int x, y, useCount;
Point(int x, int y) { this.x = x; this.y = y; }
static final 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);