Saturday 16 August 2014

Class Loaders

Whenever we compile a java file, then a public static final field named class of type 'java.lang.Class' is embedded by compiler in byte code.

In JVM, each and every class is loaded by an instance of 'java.lang.ClassLodaer' class. You can create your custom class loaders by extending 'ClassLoader' class.

The Class Loader Delegation Model
Bootstrap class loader
Whenever new JVM starts, (You can start new JVM by typing 'java ClassName'), the bootstrap class loader(also known as Primordial class loader) is responsible for loading key classes like Object class. This class loader loads the core system classes from the boot classpath,which is normally the JAR files located in the jre/lib directory. The runtime classes are packaged inside of the JRE\lib\rt.jar file. you can modify this classpath using the -Xbootclasspath command-line options.
Unlike other class loaders, the boot strap class loader can't instantiated, since it is purely native implementation.

Extension Class Loader
It is a child of Boot Strap Class loader, and it loads the classes from extension directories, normally located the jre/lib/ext directory. You can add jar files to this directory, so they will be loaded by Extension class loader. Using the system environment property java.ext.dirs you can add 'ext' folders and jar files to be loaded using extensions class loader.

System Class Loader
Also known as Application Class loader. It is responsible for loading code from the path specified by the CLASSPATH environment variable. By default, it is the parent of all the custom class loaders created by user.

Phases Of Class Loading
Ideally, there are three phases in class loading.
1.Loading
2.Linking
3.Initializing

1.Loading Phase
Loading phase locates a class file in the classpath and loads the byte code.

2. Linking Phase
It is somewhat complicated as compared to other 2 phases and further devided into 3 phases.
   2.a.Bytecode Verification
   2.b.Class Preparation
   2.c.Resolving

2.a.Bytecode Verification
Byte code verifier checks the instructions cannot perform actions that are obviously damaging. Almost all classes except system specific are verified by byte code verifier.
Below are the some of the checks performed by Byte code verifier
   a.Variables are initialized before they are used.
   b.The runtime stack doesn't overflow.
   c.Rules for accessing data or methods like private, protected, default are not violated.

You may have a doubt, why byte code verification is needed, since Compiler generates byte code after verifying your .java file only. You are correct a class file generated by a compiler for the Java programming language always passes verification. But you know, byte code is well documented code, so a person with some experience in assembly programming and a hex editor to manually produce a class file that contains valid but unsafe instructions for the Java virtual machine.

2.b. Class Preparation
Prepares the necessary data structures that represent fields,methods, and implemented interfaces that are defined within each class.

2.c. Resolving
In this phase, class loader loads all the classes referenced by this class. For example, you may use other class object in this class etc.,

3.Initializing Phase
In this phase all the staic block are executed, and static fields are initialized to their default values.

Once all the 3 phases completed successfully, then class is ready for use.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment