| Author |
Static block
|
George Fung
Ranch Hand
Joined: Jun 12, 2003
Posts: 98
|
|
static init blocks are executed at class loading time, so I think it shows "b1" first. Why it display "r1" at the beginning?
class Bird {
{ System.out.print("b1 "); }
public Bird() { System.out.print("b2 "); }
}
class Raptor extends Bird {
static { System.out.print("r1 "); }
public Raptor() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
}
}
What is the result?
r1 r4 pre b1 b2 r3 r2 hawk
|
SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJP6
To be obtained: SCEA 5
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
|
is println("b1") in a static block? I don't think so -- where's the static keyword?
|
 |
George Fung
Ranch Hand
Joined: Jun 12, 2003
Posts: 98
|
|
|
Thanks a lot.
|
 |
 |
|
|
subject: Static block
|
|
|