• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Loading  RSS feed

 
veerender reddy chama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put this clear through an example..

I've two classes namely A and B

class A
{public static final int a=10;
public static void main(String args[])
{
System.out.println(A.a);
}
}

class B{
public static void main(String args[])
{
System.out.println(A.a);
}
}

Now what I do is
step1: First compile both the classes
step2: Run class B (it displays out put as 10)
step3: Change the value of variable'a'to 12 in class A and compile it
step4: Run the Class B with out compiling it.

Check the out put it displays a as 10..

I'm in confusion whether a class A is loaded at runtime or complietime?
whlie running Class B
I would also like to know when the classes get loaded..

Please help me

regards,
veerender :roll:
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veerender,

The classes are loaded at runtime. It is the JVM's cup of tea. Actually, it is a tricky question to answer. A class will be loaded, just before accessing it. Definitely not at the compile time.

So, this question is not concerned with class loading. It is just because, class A is not compiled. So, the changes made to the code, do not reflect in the "Class".

More information on Class Loading is Here

Hope this helps.

Cheers,
Ram.
[ August 12, 2005: Message edited by: Ramaswamy Srinivasan ]
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veerender,

You are dealing with a "compile time constant". Declaring a variable as static final makes it a compile time constant. While compiling a class that uses compile time constant, the java compiler folds the constant into the byte code.

So in your example, in class B, A.a is replaced by the int value of 10 at compile time. So when you run B, the sysout argument will be 10 and not A.a. In effect class A is never loaded as it was never referenced. Only when you recomplie B will the changes made to the compile time constant be reflected.
[ August 12, 2005: Message edited by: Satish Chilukuri ]
 
veerender reddy chama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear satish,
Thank U very much, my doubt has been cleared.

Many Thanks,
Veerender
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this helps:
http://qa.jtiger.org/GetQAndA.action?qids=13&showAnswers=true
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!