This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem in Java Class Loading process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in Java Class Loading process " Watch "Problem in Java Class Loading process " New topic
Author

Problem in Java Class Loading process

Panneer Pandi Gnanadurai
Greenhorn

Joined: Apr 17, 2009
Posts: 3
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

Joined: Feb 25, 2007
Posts: 1638

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.


    apigee, a better way to API!
    Panneer Pandi Gnanadurai
    Greenhorn

    Joined: Apr 17, 2009
    Posts: 3
    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

    Joined: Jan 28, 2003
    Posts: 4167
        
      21

    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.

    Steve
    karan khosla
    Greenhorn

    Joined: Apr 27, 2009
    Posts: 25

    <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>


    Karan Check me out on http://pythonicway.blogspot.com/
    Panneer Pandi Gnanadurai
    Greenhorn

    Joined: Apr 17, 2009
    Posts: 3
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Problem in Java Class Loading process
     
    Similar Threads
    explain the concept of data hiding
    Order of Static initialization Block,Instance initialization Block and Constructor....
    Method Overlaoding questions/doubt
    init blocks example
    Polymorphism Problem