| 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
|
 |
 |
|
|
subject: static initializer in sub class and super class
|
|
|