JVM (part 4) ~ What is Class loader, How its Work
Chapter 4
In this section I am going to talk about the actual mechanism of class loader. We learned from previous articles that the class loader means loading class files into the memory area, but there is more in that process. let’s Find out what it is…
Class loader having 3 categories
- Loading
- Linking
- Initialization
LOADING
In this stage class loader duty is already compiled class files put in to the method area. when its happen before class loader runs a mechanism to check the quality of that files. Weather it’s checks,
- Fully Qualified Name
- Is it an Enum, Class or Interface
- Immediate Parent
- Instance Variable Information
After the read all information and the class loader put those files in to memory area. Each of this files loaded in to memory area JVM doing a special task, assume if we have 3 classes of A and B and there has a another one called C if that A need to refer C it create an object and JVM load class A during loading it create object from class type and the information of class C assigned to that newly created class object. if class B need to refer the information of class C JVM couldn’t create another Class object for that it just use the already created object.
LINKING
Linking also Divide into 3 Steps
- Verification
- Preparation
- Resolution
Verification
In this stage JVM doing an amazing job. it has inbuilt Verification mechanism so that no one easy to crack the java. that’s why called java is safer to execute any environment. OK let’s look that process. when you execute any program JVM has program called Bytecode Verifier using that verifier JVM checks weather this come from valid compiler, weather this had correct structure and this class file has correct formatting. if any of these are not satisfied JVM throw runtime exception called verifier exception. if you see this exception check your code again!
Preparation
If pass the verification stage next it will come to here. In this stage every created instance variable JVM assign a default value as an example think we have an int a = 5; the jvm assign to ‘0’ for that. It’s kind of a programmer friendly work.
Resolution
In the assembly language we can’t use any domain-specific words example as Employee, student. but java allows you to use it.
When it comes to JVM level jvm cant understand the domain-specific words.so that what jvm does is before it reach to the machine level JVM is replacing those Symbolic links with the Direct links.
INITIALIZATION
In JVM it is always flexible for the implementation like they can do these phases sequentially or parallel. But there is a limitation in JVM implementation specific in enforce to the implementation initialization must do before each class active. Every class must initialize before it active use. in this section JVM assign the real values into the instance variables.
There has 6 points to consider as a active use for a class in java
- new Keyword
- Invoking static method (Employee.verify();)
- Assign value for static field (Employee.code = “10”;)
- initial class — (main();)
- Instance sub class
- getInstance(Reflection api)
**** RULE : JVM has a rule if before any of these active use, it must create instance from the object. ***********