Results 1 to 5 of 5
  1. #1
    gppme is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Problem in Java Class Loading process

    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 dandling manner because I found 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

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package classLoading;

    /**classLoading.Child
    *
    * @author Jump
    */
    public class Child extends Parent implements test {
    static{
    System.out.println("START Loading Child");
    }
    static String name="CHILD";
    String id="CHILD 100";
    static{
    System.out.println("END Loading Child");
    }

    public static String getVal(){
    System.out.println("getVal in Child");
    return "RAMA "+name;
    }
    public static void main(String arr[]){

    }

    }

    /*
    Interface test
    */
    interface test{
    Child child=new Child();
    String val= child.getVal();
    }


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package classLoading;

    /**
    *
    * @author Jump
    */
    public class Parent {
    static{
    System.out.println("START Loading Parent");
    }
    static Child c=new Child();
    static{
    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);
    System.out.println("END Loading Parent");
    }




    }


    /* OUT PUT
    START Loading Parent
    getVal in Child
    at parent c.name =nullc.val=RAMA nullc.child.id CHILD 100c.child.name null
    END Loading Parent
    START Loading Child
    END Loading Child

    */




  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Posting the code in [code] tags will make it far easier to read and analyse.

    Note that your classes will be compiled to something like:
    Java Code:
    public class Parent {
        static Child c;
        static {
            System.out.println("START Loading Parent");
            c = new Child();
            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);
            System.out.println("END Loading Parent");
        }
    }
    
    public class Child extends Parent implements Test {
        static String name;
        String id;
    
        static {
            System.out.println("START Loading Child");
            name="CHILD";
            System.out.println("END Loading Child");
        }
    
        public Child() {
            super();
            id = "CHILD 100";
        }
    
        public static String getVal(){
            System.out.println("getVal in Child");
            return "RAMA "+name;
        }
    
        public static void main(String arr[]) {
        }
    }
    It would seem you've managed to construct an object before its static initializers have been run. I suspect that it is because the JVM only has a single thread for running static initializers, so although Child has been loaded, the code is still waiting to run until Parent's initializer has finished.

    The addition of the interface makes this extra funky, as I would expect it to create a stack overflowing recursive loop.
    Last edited by OrangeDog; 04-18-2009 at 01:47 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    gppme is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Problem in Java Class Loading process

    Hi,
    Thanks !

    I have updated as per your suggestion for better readability

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

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package classLoading;
    
    /**classLoading.Child
     *
     * @author Jump
     */
    public class Child extends Parent implements test {
    static{
        System.out.println("START Loading Child");
    }
       static  String name="CHILD";
          String id="CHILD 100";
    static{
        System.out.println("END Loading Child");
    }
    
    public static String getVal(){
        System.out.println("getVal in Child");
        return "RAMA "+name;
    }
    public static void main(String arr[]){
        
    }
    
    }
    
    /*
    Interface test
    */
     interface test{
        Child child=new Child();
        String val= child.getVal();
    }
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package classLoading;
    
    /**
     *
     * @author Jump
     */
    public class Parent {
    static{
        System.out.println("START Loading Parent");
    }
        static Child c=new Child();
    static{
      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);
        System.out.println("END Loading Parent");
    }
    
    
    
    }

    /* OUT PUT
    START Loading Parent
    getVal in Child
    at parent c.name =nullc.val=RAMA nullc.child.id CHILD 100c.child.name null
    END Loading Parent
    START Loading Child
    END Loading Child

    */

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I was afraid you'd repost. Just go back and edit your original post (preferably without the template comments) then remove this one.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. class loading
    By purejoker in forum AWT / Swing
    Replies: 1
    Last Post: 01-20-2009, 12:09 PM
  2. Factory loading class...help?
    By prabhurangan in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2008, 02:20 PM
  3. Loading a class within a different class
    By nick2price in forum New To Java
    Replies: 1
    Last Post: 05-30-2008, 07:19 AM
  4. How can I set a time out on the loading process
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 04:54 AM
  5. Help with class loading in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •