aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another thread from Dan's mock exam 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 "another thread from Dan Watch "another thread from Dan New topic
Author

another thread from Dan's mock exam

Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
class A {
A() {
System.out.println("CA");
}
{
System.out.println("IA");
}
static {
System.out.println("SA");
}
}
class Test extends A {
Test() {
System.out.println("CB");
}
{
System.out.println("IB");
}
static {
System.out.println("SB");
}
public static void main(String[] args) {
new Test();
}
}
Dan , would you tell me why? thx


SCJP1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
why what?


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Again this is how I summerized Dan's explanation on this topic:
On instantiating an object of a certain type, the static initializers of the super classes are executed first starting with the highest class in the hierarchy, then the static initializer of the class of the object itself is executed.
Then because of an implicit or explicit super() call in the subclass constructor, the instance initializers and the constructors of the super classes are executed in couples (instance initializer & constructor of each class) also starting with the highest class in the heirarchy then at the end the instance initializer and constructor of the class of the object itself are executed.
* Instance initializers are always executed before the constructors.
* If there is more than one block of static initializers in the body of the class, they are executed according to the order they appear in the class body. Using variables - that are declared in a later initializer - in an earlier one will cause a compile time error. Same goes for inistance initializers.
HTH
[ October 15, 2002: Message edited by: Alfred Kemety ]
[ October 15, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
thank you, Alfred Kemety.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another thread from Dan's mock exam