aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another K&B mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another K&B mock question" Watch "another K&B mock question" New topic
Author

another K&B mock question

gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
Hi all,
I can't understand the result of the following code:



The result o f the run is :
r1 r4 pre b1 b2 r3 r2 aigle

I don't understand why the static code in class Rapace is executed before the instantiation of Aigle Object.
This means that all the static code inside a project is executed during compilation?
Or there's something related to inheritance between Oiseau/Rapace/Aigle objects since Aigle is inside the main method?

Thanks for clarifying my shameful confusion !

Gianni
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
Static code is executed (once) when the class is loaded into the JVM


SCJP2 96%
gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
Thanks Rachil.
And when does a class is loaded in JVM?
Do all the classes of a project are loaded in JVM before the execution of main method?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

I am not positive, but i BELIEVE that each class is loaded just before it is needed for the first time. you may have a branch in your code that never needs certain classes, so (again, i think) those will never be loaded.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
in fact if I add a class that has nothing to do with the others, the static code is not executed :


"reptile" is not printed out.

So maybe the jvm is so intelligent to know, before invoking main method, all the classes that will be directly involved in the execution...
is that right?
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
No the code is executed when the JVM needs the class and will then load that class into memory. Your reptile class is never used and will so never loaded into memory.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Fred Rosenberger:
I am not positive, but i BELIEVE that each class is loaded just before it is needed for the first time...

Exactly. Any of the following can cause a class to load (if not already loaded):
  • Accessing a static member
  • Creating a new instance
  • Calling Class.forName("ClassName")


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
     
    wood burning stoves
     
    subject: another K&B mock question