Results 1 to 5 of 5
- 04-18-2009, 01:12 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
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
*/
- 04-18-2009, 01:23 AM #2
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:
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.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[]) { } }
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
- 04-18-2009, 01:32 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
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
*/
- 04-18-2009, 01:46 AM #4
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
- 04-18-2009, 04:04 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
class loading
By purejoker in forum AWT / SwingReplies: 1Last Post: 01-20-2009, 12:09 PM -
Factory loading class...help?
By prabhurangan in forum Advanced JavaReplies: 2Last Post: 10-14-2008, 02:20 PM -
Loading a class within a different class
By nick2price in forum New To JavaReplies: 1Last Post: 05-30-2008, 07:19 AM -
How can I set a time out on the loading process
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 04:54 AM -
Help with class loading in java
By mathias in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks