File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Java Class Loading process

 
Panneer Pandi Gnanadurai
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am trying to understand how ClassLoading is handled by java specifically during recursive class declarations. Here is the example and out put,could you please any one justify the output with valid reason and understanding.

From my understanding , Java handling this in a dangling manner because it creates an object with one of its class variable of String type as null but in code it has a valid string literal assignment



OUTPUT :

START Loading Parent
getVal in Child
at parent c.name=null, c.val=RAMA null, c.child.id=CHILD 100, c.child.name=null
END Loading Parent
START Loading Child
END Loading Child
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is more about initialization sequence. Classloading refers to a different concept.

There are a few things here:
  • All members variables of an interface are by default static and final
  • In the initialization sequence, first all the static members of all the parent class/interface are loaded in the sequence they appear in the class definition.


  • The above explains the output with just one exception that you are initializing a child instance in one(both) of the parents.
    Even if you do this, the rule in point 2 above can not be violated and so the jvm initializes all the static fields of the parent and the static fields of the child are always printed as null. The instance field is *not* null because the instance is created and thus the instance level field have to be initialized.

    Having said the above, you must not do this kind of thing in actual code (i.e. to instantiate a child in the parent static block), as it adds to confusion.

    Moving to intermediate.
     
    Panneer Pandi Gnanadurai
    Greenhorn
    Posts: 3
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    Thanks for your comments.

    Thanks for remanding me class loading and initialization sequence is different concept. However I do remember initializing class variable (static variable) is part of class loading process.

    I am not agree with your reply due to following reasons.

    As per the output I have got following is the class loading sequence

  • Loading Parent Class
    Loading Child Class


  • But when I run the program using “java –verbose ” I found the loading sequence is

  • Loading test Interface
    Loading Parent Class
    Loading Child Class



  • Here is my key doubts are.

    1. In parent class how it can create an instance for Child with out loading “Child Class”
    2. How an instance variable can be assigned value before assigning value to class variable (static)?

    I am adding JAVA class loading output for your reference.




    Objective of this program is to clearly understand how class loading works , not anything else.


    --Thanks
    Panneer PAndi
     
    Steve Luke
    Bartender
    Pie
    Posts: 4181
    21
    IntelliJ IDE Java Python
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Panneer Pandi Gnanadurai wrote:...
    Here is my key doubts are.

    1. In parent class how it can create an instance for Child with out loading “Child Class”


    It can't. As you can see from your output, the 3 classes are loaded before the initialization begins.

    Panneer Pandi Gnanadurai wrote:
    2. How an instance variable can be assigned value before assigning value to class variable (static)?


    Nitesh already explained that to you.
     
    karan khosla
    Greenhorn
    Posts: 25
    IntelliJ IDE Java Ubuntu
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    <pre>Initialization Flow for this Program

    Key Point throughout the Process
    - static executes once in the lifetime of the program.
    - call to Construtor means all the instance variables of the class got initilized.
    - Java Compiler is very smart in the sense that it inserts some instructions that helps VM.

    1. On terminal Window > java Child
    - loads Child.class
    - loads Parent.class
    - loads test.class

    2. In Child Header
    1. First, A call to Parent class is made.
    2. Second, A call to the test interface is made.

    1. First, A call to Parent class is made.

    - Prints "START Loading Parent".
    - call to Child class constructor -- Refer Key Point - Point 2.
    static Child c = new Child() ;
    - In Early Stage (Compile Time) When the below line gets encountered -- Refer Key Point 3
    System.out.println("at Parent c.name = " + c.name + ", c.val = " + c.val + " c.child.id = " + c.child.id + " c.child.name = " + c.child.name) ;
    - Behind the scene -- Compiler
    - inserts exactly 3 lines before the Print Statement with a call to test interface.
    - First for c.val
    - Second for c.child.id
    - Third for c.child.name
    - So the VM executes these three lines before the Print Statement
    - the first call, that is, c.val executes the constructor of Child class again.
    - but in the Second and Third call the constructor of Child class won't be executed -- Refer Key Point 1
    - Prints at Parent c.name = null, c.val = RAMA null c.child.id = CHILD 100 c.child.name = null
    - Prints "End Loading Parent".

    2. Second, A call to the test interface is made.

    - nothing to do there -- Refer Key Point 1.

    3. Call return to Child class

    - Prints "START Loading Child".
    - Prints "End Loading Child".

    And also refer to Mr. Nitesh Kant Comment for better understanding.
    I Hope this information is helpful to you.
    </pre>
     
    Panneer Pandi Gnanadurai
    Greenhorn
    Posts: 3
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dear Nitesh Kant &karan khosla ,

    I highly appreciate all your great answer to solve my doubts.

    Now I understood very clearly the problem.

    Actually, I misunderstood that “once the is loaded , it will start initializing static fields before loading another class”.

    Now form you reply and some experiments I understood the followings

    1. Class Loading and static variable Initialization are different activities
    2. Class loading will be performed first then Initialization of static variables will happen
    3. Class Loading is creating instance of java.lang.Class to represent current class.
    4. Following is the sequence of activities for loading a class
    a. Load all super interfaces
    b. Load all super classes
    c. Invoke static initialization of super class (SUPER Interface won’t get initialized)
    d. Invoke static initialization of current class


    I would like to know the answer for the following doubt.

    1. Where the created instance of java.lang.Class is loaded ?
    2. Where the java runtime classes will get loaded?

    I assume it is Heap and Method Area.

    --Panneer Pandi
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic