Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

when is a class in general loaded by the JVM

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Right! We're on it! Let's get to work tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic