In this article, let’s discuss about the functionalities happen inside the Class Loader. Also, there are 2 types of class loaders known as Bootstrap class loader and Custom-defined class loader.
There are major 3 responsibilities which are performed by JVM Class Loader. They are,
1. Class Loading
Major responsibilities of Class loading: Take the class name of compiled Java class and put it into the memory area. But other than that Class loader is responsible for,
i. Read fully qualified class name,
ii. Read variable information,
iii. Read about the immediate parent information,
iv. Check whether if it is a class, interface or enum.
Other than that, JVM creates class type object and assign these information to that class during the class loading mechanism.
2. Class Linking
In this phase, major 3 steps are involved. They are,
In here, the byte code verifier resides in JVM check whether our Java code is secure to execute or not. It checks,
- whether the class file has proper structure,
- whether the class file has proper format,
- whether class file is compiled by the valid compiler.
However, if the particular class file is unable to comply with any of these rules, then it creates run-time exception called verify exception.
It assigns the default values for instance or static variables in class file. For an example,
- If it is integer then assign as 0,
- If it is boolean then assigned as false,
- If it is an object then assigned as null.
Java is really programmer-friendly program since it allows to use domain-specific words while writing codes.
But these words can not be understood by our operating system in order to compile it properly. In here what JVM does is before these domain-specific names/ words reach to the machine level converted to the direct links understood by our machine.
For an example, let’s say that we create an object from the particular class called Employee and we invoked a method by using that object. In here, what JVM does is it replace a memory location for everywhere we use Employee object.
3. Class Initialization
In this mechanism, the major responsibility of JVM is to assign the real value by replacing default value.
Every class should be initialized before it’s in active use. That is a rule of JVM.
There are 6 ways which specifies as Java class is in active use. They are,
- If we use “new” keyword to instantiate an object from the class,
- If we invoke static method,
- If we assign values for static variables,
- If your class has a main method,
- If we use Reflection APIs to create objects,
- If we instantiate sub class.
There are 4 ways we can use to initialize a class. They are,
- Use “new” keyword,
- Use clone();
- Use reflection API
- Use IO.ObjectInputStream class
Also, assigning real value or initial value is differ from the way we used to initialize a class.
For an example, if we use clone method [clone();] to create an object, JVM reads object value of its parent to assign the initial value. If we use IO.ObjectInputStream, then it will assign non-transient variables to assign initial values.
|What is the reason when you initialized a child class the parent class get called / initialized ?
Let’s refer following structure.
Employee extends Human
Employee (int a)
- When JVM compile this class, it will create a default constructor although the programmer does not create it. As well, JVM creates a method called init [init()] for each constructor.
- Also, there is a rule in java parent class must be visible its child classes.
- So, what is inside of init method is depends on class structure.
- As an above example, let’s say one of the constructors with parameter (the second constructor in this class structure) contain “this” keyword. In such a situation, the init method of that constructor enforces to have following requirements.
Has information about default constructor’s init method,
Has byte codes of particular constructor.
- But the constructor with parameter does not occupy this(), then the init method of that constructor enforces to have followings.
It has codes to invoke init method of parent class’s default constructor,
Has codes to initialize instance variables,
Has byte codes of particular constructor.
- That means, in a compile time, JVM is putting reference to its super class / parent class constructor.
So, I hope you can get the better insight about the JVM by using this article. Let’s discuss about the Memory area of JVM through my next article.
Stay Safe !!!