• Post Reply Bookmark Topic Watch Topic
  • New Topic

when is a class in general loaded by the JVM  RSS feed

 
shobhit garg
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Acc to Head first java .
static blocks are executed when the class is loaded even before a constructor is called .Now when exactly does the jvm load a class ?

Thanks
shobhit
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When needed.
That's essentially all you can say.

It might be the first time you instantiate it, but it could conceivably also preload classes it knows will be needed at some point in the future.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some thing you can do that you know will cause a class to load if it hasn't already:

new ClassName()
ClassName.staticMember
Class.forName( "classname" )
aClassLoader.anyNumberOfMethods ...

I'm sure there are more. What brought up the question? I'm going to feel bad if I just did your homework, ya know.
 
shobhit garg
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
There are some thing you can do that you know will cause a class to load if it hasn't already:

new ClassName()
ClassName.staticMember
Class.forName( "classname" )
aClassLoader.anyNumberOfMethods ...

I'm sure there are more. What brought up the question? I'm going to feel bad if I just did your homework, ya know.


it was plain curiosity that brought up this question ..thanks
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
There are some thing you can do that you know will cause a class to load if it hasn't already:

new ClassName()
ClassName.staticMember
Class.forName( "classname" )
aClassLoader.anyNumberOfMethods ...

I'm sure there are more. What brought up the question? I'm going to feel bad if I just did your homework, ya know.


To point out a gotchya that I've seen a few times - ClassName.staticMember will not cause a class to load if that static member evaluates to a constant expression.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM will load your class first and initialise it when you try to call any member of class.

e.g., when you run a java program by....

java Test

It means you want to call Test.main(String [] args).

So JVM will first load your class and then it will initialize it.


Naseem
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassName.staticMember will not cause a class to load if that static member evaluates to a constant expression


That's spooky. Can we infer the compiler gets the "constant" expression at compile time and builds it into the calling class? And the compiled code could fail to pick up changes in the other class?
 
shobhit garg
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have another doubt regarding class getting loaded...
when we say objects are created it means they are being allocated memory on heap..
local vars find place in the stack ...
now what does loading a class exactly means and where in the memory a class is loaded
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!