`

Something about JVM class loading and initialization

    博客分类:
  • Java
 
阅读更多

Class loading stages:

  • Loading
  • Linking
    • Verification
    • Preparation
    • Resolution
  • Initialization

 

Class Loader:

 

 

 

Class loading mechanism:

  • parent delegation model

     The delegation model requires that any request for a class loader to load a given class is first delegated to its parent class loader before the requested class loader tries to load the class itself. The parent class loader, in turn, goes through the same process of asking its parent. This chain of delegation continues through to the bootstrap class loader (also known as the primordial or system class loader). If a class loader's parent can load a given class, it returns that class. Otherwise, the class loader attempts to load the class itself.

 

When a Class is loaded:

 

"The Lifetime of a Class," different implementations of the Java virtual machine are permitted to perform resolution at different times during the execution of a program. An implementation may choose to link everything up front by following all symbolic references from the initial class, then all symbolic references from subsequent classes, until every symbolic reference has been resolved. In this case, the application would be completely linked before its main() method was ever invoked. This approach is called early resolution. Alternatively, an implementation may choose to wait until the very last minute to resolve each symbolic reference. In this case, the Java virtual machine would resolve a symbolic reference only when it is first used by the running program. This approach is called late resolution. Implementations may also use a resolution strategy in-between these two extremes.

Although a Java virtual machine implementation has some freedom in choosing when to resolve symbolic references, every Java virtual machine must give the outward impression that it uses late resolution. No matter when a particular Java virtual machine performs its resolution, it will always throw any error that results from attempting to resolve a symbolic reference at the point in the execution of the program where the symbolic reference was actually used for the first time. In this way, it will always appear to the user as if the resolution were late. If a Java virtual machine does early resolution, and during early resolution discovers that a class file is missing, it won't report the class file missing by throwing the appropriate error until later in the program when something in that class file is actually used. If the class is never used by the program, the error will never be thrown.

 

When a Class is initialized:

After class loading, initialization of class takes place which means initializing all static members of class. A Class is initialized in Java when :

  • An Instance of class is created using either new() keyword or using reflection using Class.forName(), which may throw ClassNotFoundException in Java.
  • An static method of Class is invoked.
  • An static field of Class is assigned.
  • An static field of class is used which is not a constant variable.
  • If Class is a top level class and an assert statement lexically nested within class is executed.

Reflection can also cause initialization of class. Some methods of java.lang.reflect package may cause class to be initialized. JLS Strictly says that a class should not be initialized by any reason other than above

 

Rules of class initialization:

Now we know what triggers initialization of a class in Java, which is precisely documented in Java language specification. Its also important to know in which order various fields (static and non static), block (static an non static), various classes (sub class and super class) and various Interfaces (sub interface, implementation class and super interface) is initialized in Java. Here are some of the rules of class initialization in Java:

  • Classes are initialized from top to bottom so field declared on top initialized before field declared in bottom
  • Super Class is initialized before Sub Class or derived class in Java
  • If Class initialization is triggered due to access of static field, only Class which has declared static field is initialized and it doesn't trigger initialization of super class or sub class even if static field is referenced by Type of Sub Class, Sub Interface or by implementation class of interface.
  • Interface initialization in Java doesn't cause super interfaces to be initialized.
  • Static fields are initialized during static initialization of class while non static fields are initialized when instance of class is created. It means static fields are initialized before non static fields in Java.
  • Non-static fields are initialized by constructors in Java. Sub class constructor implicitly call super class constructor before doing any initialization, which guarantees that non static or instance variables of super class is initialized before sub class
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics