8.3.3
Examples of Field Declarations
CLASSES
If a reference by simple name to any instance variable occurs in an initializa
tion expression for a class variable, then a compile time error occurs.
If the keyword
this
( 15.7.2) or the keyword
super
( 15.10.2, 15.11)
occurs in an initialization expression for a class variable, then a compile time
error occurs.
(One subtlety here is that, at run time,
static
variables that are
final
and
that are initialized with compile time constant values are initialized first. This also
applies to such fields in interfaces ( 9.3.1). These variables are constants that
will never be observed to have their default initial values ( 4.5.4), even by devious
programs. See 12.4.2 and 13.4.8 for more discussion.)
8.3.2.2
Initializers for Instance Variables
A compile time error occurs if an initialization expression for an instance variable
contains a use by a simple name of that instance variable or of another instance
variable whose declaration occurs to its right (that is, textually later) in the same
class. Thus:
class Test {
float f = j;
int j = 1;
int k = k+1;
}
causes two compile time errors, because
j
is referred to in the initialization of
f
before
j
is declared and because the initialization of
k
refers to
k
itself.
Initialization expressions for instance variables may use the simple name of
any
static
variable declared in or inherited by the class, even one whose declara
tion occurs textually later. Thus the example:
class Test {
float f = j;
static int j = 1;
}
compiles without error; it initializes
j
to
1
when class
Test
is initialized, and ini
tializes
f
to the current value of
j
every time an instance of class
Test
is created.
Initialization expressions for instance variables are permitted to refer to the
current object
this
( 15.7.2) and to use the keyword
super
( 15.10.2, 15.11).
8.3.3 Examples of Field Declarations
The following examples illustrate some (possibly subtle) points about field decla
rations.
150
footer
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