Class loading is a process which is done by the JVM after compilation. When we are compiling then the task of compiler is to check the syntax error which is called as compilation and hence after successful compilation a .class file is created which is a compiled file and now before processing that class, it is first loaded by the JVM and while loading .class file JVM will allocate the memory for the static variable and this is why for any static member only once the memory is created and it will remain throughout the program and can be accessed by any object.
Steps followed while class loading by JVM.
1> While class loading JVM will instantiate all the static member.
2> It will try to execute any static block available in the program.
3> It will initialize all the instance member through the default constructor by creating the object.
4> It will execute any instance block available in the program but the instance block is never going to initialize the instance member.
Lets see and example to clear out all the above points.
In the above program we can see that
1> X is a instance variable that means it must be initialized while creating the object.
2> Y is a static variable that means it will initialized while loading the class by the JVM.
3> There is a static block execute after initializing the static variable.
4> Instance block is being executed by the JVM.
Diagram representing task of JVM while class loading
Note : While class loading JVM will keep all the information about the static variable, static block , static method, instance variable, instance block, instance method, constructor etc in the memory location of the main memory called as method area.
After initializing all the static member now JVM will allocate memory for the object (if user write valid syntax for object creation in the program). If user don't write any code for object creation then JVM is not going to allocate any memory for any Object defined in the program.
Steps followed while class loading by JVM.
1> While class loading JVM will instantiate all the static member.
2> It will try to execute any static block available in the program.
3> It will initialize all the instance member through the default constructor by creating the object.
4> It will execute any instance block available in the program but the instance block is never going to initialize the instance member.
Lets see and example to clear out all the above points.
public class prog {
int x ; // instance variable
static int y ; // static variable
static{ // static block
System.out.println(z);
}
{ // instance block
System.out.println(x);
}
public static void main(String sush[]){
prog p = new prog (); // object creation
}
}
In the above program we can see that
1> X is a instance variable that means it must be initialized while creating the object.
2> Y is a static variable that means it will initialized while loading the class by the JVM.
3> There is a static block execute after initializing the static variable.
4> Instance block is being executed by the JVM.
Diagram representing task of JVM while class loading
Note : While class loading JVM will keep all the information about the static variable, static block , static method, instance variable, instance block, instance method, constructor etc in the memory location of the main memory called as method area.
0 comments:
Post a Comment