EXECUTION
When Initialization Occurs
12.4.1
12.4   Initialization of Classes and Interfaces
Initialization of a class consists of executing its static initializers and the initializ 
ers for
static
 fields (class variables) declared in the class. Initialization of an
interface consists of executing the initializers for fields (constants) declared there.
Before a class is initialized, its superclass must be initialized, but interfaces
implemented by the class need not be initialized. Similarly, the superinterfaces of
an interface need not be initialized before the interface is initialized.
12.4.1   When Initialization Occurs
A class or interface type
T
 will be
initialized
 at its first
active use
, which occurs if:
T
 is a class and a method actually declared in
T
 (rather than inherited from a
superclass) is invoked.
T
 is a class and a constructor for class
T
 is invoked, or
U
 is an array with ele 
ment type
T
, and an array of type
U
 is created.
A non constant field declared in
T
 (rather than inherited from a superclass or
superinterface) is used or assigned. A
constant field
 is one that is (explicitly or
implicitly) both
final
 and
static
, and that is initialized with the value of a
compile time constant expression ( 15.27). Java specifies that a reference to a
constant field must be resolved at compile time to a copy of the compile time
constant value, so uses of such a field are never active uses. See  13.4.8 for a
further discussion.
All other uses of a type are
passive uses
.
The intent here is that a class or interface type has a set of initializers that put
it in a consistent state, and that this state is the first state that is observed by other
classes. The static initializers and class variable initializers are executed in textual
order, and may not refer to class variables declared in the class whose declarations
appear textually after the use, even though these class variables are in scope
( 8.5). This restriction is designed to detect, at compile time, most circular or oth 
erwise malformed initializations.
As shown in an example in  8.5, the fact that initialization code is unrestricted
allows examples to be constructed where the value of a class variable can be
observed when it still has its initial default value, before its initializing expression
is evaluated, but such examples are rare in practice. (Such examples can be also
constructed for instance variable initialization; see the example at the end of
 12.5). Java provides the full power of the language in these initializers; program 
mers must exercise some care. This power places an extra burden on code genera 
tors, but this burden would arise in any case because Java is concurrent ( 12.4.3).
223






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