• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When do classes get loaded into the JVM?

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I got the wrong answer to Q6 in the Self Test Question for Chapter 3 of K&B 6. The question tests your knowledge of in what order static and non-static initialization blocks and constructors will run. I knew that the static init blocks would run before the non-static init block and constructors, but I wasn't sure of exactly when classes get loaded into the JVM, so I didn't know if the println() statement on the first line of main in a subclass would run before the static int blocks in the super class.




According to the answers, the static init blocks did run before the print() method on the first line of main. Cool, but my question is:

Which classes get loaded into the JVM and when?

Did the static init blocks in the Raptor class only run because the Hawk class had to be loaded to run the main method and that meant that all it's superclasses had to be loaded?

Or do absolutely all classes that are in java.lang get loaded and any others that have been declared as being imported?

Do a class and it's superclasses only get loaded just before they're needed in the code execution, or does it happen as soon as I call "java MyProgram"?

Also, what does it mean for a class to be loaded? What actually happen? Is it that a Class object gets created for that class?

As always thanks for your replies in advance.

Joe
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Joe

Just see, where main method is being declared, if it is decalared in a class that extends some class....then all classes are loaded from Object class to your class....
What happens after that , you know it...

As a Test: Please do check the behavior by declaring the main method in test class....!!!
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Excellent. Thanks Sahil. With the addition of the TestLoading class, "pre " is now printed before anything else.

Thanks for your help.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Kapoor wrote:Hello Joe

Just see, where main method is being declared, if it is decalared in a class that extends some class....then all classes are loaded from Object class to your class....
What happens after that , you know it...

As a Test: Please do check the behavior by declaring the main method in test class....!!!




i understand everything from above.....but the one thing which comes to my mind whn i see question like this.......

is that.........why anonymouse block runs befor default constructor.
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that we can have more than one class loader loading different classes simultaneously.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naresh Chaurasia wrote:Is it possible that we can have more than one class loader loading different classes simultaneously.



Sure. Its possible
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak, I know we can do it . But can you help me understand how do we really do it.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why anonymouse block runs befor default constructor.


well this is the rule
the init blocks are executed after the super constructor has completed
and then the constructors are executed
init blocks run in the order they appear in the code
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naresh Chaurasia wrote:Deepak, I know we can do it . But can you help me understand how do we really do it.



Err... that is what you wanted to know. But anyway, you can instantiate a new class loader and assign it a parent class loader. Classes will be loaded based on some sort of approach and there are rules as to how classes get loaded; how equals works on classes loaded by different loaders etc etc. These details are beyond the scope of SCJP
 
reply
    Bookmark Topic Watch Topic
  • New Topic