How does JAVA ClassLoader work?



How does JAVA ClassLoader work?..

Answer / javamasque

The java ClassLoader works in hierarchical manner.
There are 3 phases in class loader execution.
i. Loading phase: Finding and importing the binary data into JVM
ii. Linking phase: Linking is the process of incorporating the binary type data into the runtime state of the virtual machine. Linking is divided into three sub-steps: verification, preparation and resolution.
iii. Initializing phase: Invoking Java code that initializes class variables to their proper starting values.

1. Loading Phase: Classloader fallows 3 principles during this phase, the three principles are as below

i. Delegation Principle: The delegation of class load request executes in two ways

Namespace search: This search took place first from System Classloder (Child) to Bootstrap Classloader (Parent) for already loaded class.
Class loading request comes to System Classloader, this Classloader finds in own namespace area in constant pool in heap for the reference of requested class if found return fully qualified class name to JVM else it delegates the load request to its parent for the namespace search and finally the load request reaches to Bootstrap Classloader. The Bootstrap Classloader finds in its own namespace area in constant pool in heap for the reference of requested class, if found returns fully qualified class name to JVM.

CLASSPATH search: This search starts as Bootstrap Classloader fails to get class details in its own namespace and this classpath search recursively reaches to System Classloader.

As Bootstrap Classloader fails to get class details in its own namespace, it finds the class in its own classpath, if found, imports binary (reads .class) details and keep track in own namespace area otherewise throws ClassNotFoundException with stacktrace details to child, If exception from parent then the Extension Classloader finds in its classpath if found, imports binary data and keep track in own namespace area otherwise throws ClassNotFoundException with stack trace details to child. If exception from parent, the System Classloader tries to import binary data from CLASSPATH is set as environment variable, if fails throws ClassNotFoundException with stack trace details JVM.

After importing binary data, each Classloader trace into the binary data and pulls binary data of reference type such as superclass, field type, method return type, parameter type etc respectively and prepares internal structure in constant pool of method area in heap. During creation of internal structure, the Class File Verifier makes sure the class file (byte codes) is safe to use with the help of various checks as per Java specification and the proper class file (byte code) is loaded into JVM (method area in heap) after successful verification.

During the loading phase the symbolic references between dependent and dependency are properly organized in constant pool by JVM

Finally create instance of java.lang.Class of each Class type with act as interface between program and internal structure in heap.

ii. Visibility Principle: According to this principle, child Classloader can see all classes loaded by its parent Classloader but not vice-versa. If we try to load class explicitly, it will through ClassNotFoundException.
public class ClassLoaderTest {
public static void main(String args[]) {
try {
//printing ClassLoader of this class
System.out.println("ClassLoaderTest.getClass().getClassLoader() : "
+ ClassLoaderTest.class.getClassLoader());
//trying to explicitly load this class again using Extension class loader
Class.forName("test.ClassLoaderTest", true
, ClassLoaderTest.class.getClassLoader().getParent());
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassLoaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

iii. Uniqueness Principle: This principle says, same class can’t be loaded by parent and child Classloader. Class loading reference should be unique throughout all individual Classloader namespaces.

2. Linking Phase: This phase performs three main sub-phases as below
i. Verification: During this sub-phase, the symbolic references are verified by locating classes, interfaces, fields, and methods stored in the constant pool. When the virtual machine searches for a symbolically referenced entity (type, field, or method), it first make sure the entity exists. If the virtual machine finds that the entity exists, it must further check that the referencing type has permission to access the entity, given the entity has access permissions and other checks are done.

ii. Class Preparation: During this sub-phase, the binary data along with all the reference type have already imported and loaded as internal structure in constant pool (namespace area) of JVM and successfully completed all required verifications and is ready for preparation. Now JVM allocates memory for the class variables and sets them to default initial values. The class variables are not initialized to their proper initial values until the initialization phase. (No Java code is executed during the preparation step.) During preparation, the Java Virtual Machine sets the newly allocated memory for the class variables to a default value determined by the type of the variable.

iii. Resolution: Resolution is the process of locating classes, interfaces, fields, and methods referenced symbolically from a type in constant pool, and replacing those symbolic references with direct/pointer references. This phase of linking is optional until and unless each symbolic reference is first used by the program.

3. Initializing Phase: Initialization is required to ready a class or interface for its first active use, It is the process of setting class variables to their proper initial values as set by programmer.

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is a java predicate?

0 Answers  


Can I declare a class as private?

0 Answers  


Does 'true' and 'false' are keywords?

5 Answers  


What is the difference between normal report & matrix report?

0 Answers  


what is different between static and non static methods ,using example

2 Answers  






When should I use singleton pattern?

0 Answers  


What is the difference between yielding and sleeping in java programming?

0 Answers  


is JVM platform dependent or independent..?

11 Answers   IBM, Tech Mahindra,


Differentiate between class and structure.

0 Answers   Amdocs, Motorola,


How do you trim a space in java?

0 Answers  


What is jit compiler in java?

0 Answers  


what is the role of xml in core java?? and how we can use it?? can somebody give a sample program with explanation and from where i can read more about xml?????

0 Answers   HCL, SAP Labs, Vital Soft,


Categories