Thursday, June 7, 2012

Jvm not only read Bytecode created by compiler and run it,apart from it, it do a lot work for us

while running this program i get stuck.
class A{
    static{
        System.out.println("A");
    }
    public static void main(String ar[]){
        System.out.println("A's main ");
    }
}
class B extends A{
    static{
        System.out.println("B");
    }
}
public class C {
    static{
        System.out.println("C");
    }
    public static void main(String[] args) {
        B.main(null);

    }

}

Now lest run the class C and see what the o/p is:
C
A
A's main 

As per java concept whenever class loaded in memory its static block get executed.its fine.....
now just move step by step for above program.
first class C is loaded in memory so class C static block execute first, then main() method get executed
now let see what inside main method of class C?
B.main(null)
ok

now what class B have?
have one static block only inside and extending class A..ok
now what class A have?
have a main method and one static block.here everything is fine
through class B we reaching to class A's main method because class B extending class A fine....
but when i check the output i have seen class B static block is not get executed means class B is not loaded to memory but without loading class B how class A get loaded..????
i discuss with many people, some say compiler do some edition when we compile the class.but what compler do..???
compiler never have a right to delete our single character then how class B get erased/override and if B is not erase/override then why class B is not loaded on the line executing"B.main(null)"
so by saying that compiler take care of this is absolutely wrong because compiler ony make our word meaningfull like for String it make java.lang.String and perform some complusray task if we not did like extending Object class and creating default constructor if we not create
Then i study JVM ,to know what actual JVM do
and  finally get the answer for this issue

Class are made available to a running program by the Java Virtual Machine. This is done through a process of loading, linking, and initialization. Loading is the process of locating the binary representation of a type and bringing it into the JVM. Linking is the process of taking the type and incorporating it into the runtime state of the JVM so that it can be executed. Initialization is the process of executing the initializers of a type (static initializers for classes; static field initializers for classes and interfaces). When a type becomes unreachable (i.e. the running application has no references to the type), it becomes eligible for garbage collection. This collection is called type unloading.

Here are the step follow when we run our any java program:
   1. Given a class's fully qualified name, produce a stream of binary data that represents that type
   2. Parse the stream of binary data produced in step #1 into internal structures in the method area of the JVM. The method area is a logical area of memory in the VM that is used to store information about loaded types.
   3. Create an instance of class java.lang.Class that represents the class indicated in step #1

According to this lets explain tha above details
B.main(null) come under binary data as mention in step #1
while parsing the data JVM link main(null) directly to class A becuase Main method exist in class A and load only class A
because of that class B not get overloaded


If you really enjoyed this
give feedback
thanks...... :)