aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static initializer in sub class and super class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "static initializer in sub class and super class" Watch "static initializer in sub class and super class" New topic
Author

static initializer in sub class and super class

harry psll
Greenhorn

Joined: Jun 17, 2004
Posts: 17
hai
this question is from Dan Chisholm's mock exam

class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {
B b = new B();
}}

Answer is, it prints SA SB CA CB
I got the same when I compile and run the code.
can any one explain me,why the output is not SA CA SB CB
Thanks in advance
Haripriya
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Haripriya,

Thank you for using my exams.

Static initialization of class B must be completed before the main method can run; SA SB is printed even before the main method runs. CA CB is printed when the code contained in the body of the main method creates an instance of class B.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
harry psll
Greenhorn

Joined: Jun 17, 2004
Posts: 17
thanks Dan for the reply.
it helped me.

haripriya
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static initializer in sub class and super class
 
Similar Threads
Dan Chisholm exam - Topic Constructors
java instance creation
another thread from Dan's mock exam
K&B Book not complete ???
Constructor and Static initialization order