File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static initializer... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static initializer..." Watch "Static initializer..." New topic
Author

Static initializer...

Damodar Mukherjee
Ranch Hand

Joined: Mar 20, 2007
Posts: 135
This code produce the output
b1 r1 r4 pre hawk and if I do noe extend Raptor to A then output is nothing!!! Why??? We know when a class forms the Static part will initialize but if we donot extend Raptor then also we should get the output...

Can any one please explain this...






SCJP 1.5, SCWCD 1.4, Next... SCBCD5
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

When you are not extending Raptor then why in the world will the static block be executed


apigee, a better way to API!
Damodar Mukherjee
Ranch Hand

Joined: Mar 20, 2007
Posts: 135
So why it is executing when A is extending it???
And why not if other class in the same .java file extending some other class???
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
A class is initialized before a static method it declares is invoked, and all of its superclasses are initialized if they are not already initialized.
Paras Jain
Ranch Hand

Joined: Feb 26, 2005
Posts: 137
You mean to say you are not able to see even "pre" and "hawk" printed when you don't extend from Raptor?


Paras Jain
SCJP 5.0
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
A Static block is executed only when a class is loaded in JVM.

Now if you are not extending Those classes then, those classes will not be loaded into JVM. And if those classes are not loaded in JVM, then obviouslly static block is not going to be executed. And that is what happening when you extends and dont extend Raptro Class.

Note : A class is loaded into JVM only once when you first create instance of it. After that if you make second instance of that class, static block of that class won't be executed becos that class already exist in JVM.


I hope i am making my self Clear.



======================================
Enjoy(ing) The World of Programming...
======================================
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

A class is loaded into JVM only once when you first create instance of it.

Also, when you refer to any static fields/methods of the class, or when you load it via reflection.
gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
Hi,
I posted a similar problem some days ago,
here you are:
http://www.coderanch.com/t/261887/java-programmer-SCJP/certification/another-mock

Hope it helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static initializer...