| Author |
static initialization block
|
Jacky Zhang
Greenhorn
Joined: Sep 17, 2006
Posts: 18
|
|
result: cbaa1b1c1 it this correct? thanks in advance EDIT by mw: Added Code Tags to original poster's indentation. [ September 18, 2006: Message edited by: marc weber ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jacky Zhang: ...result: cbaa1b1c1 it this correct? ...
No, but you're close. Have you tried running it?
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Sureshkumar Chinnappan
Greenhorn
Joined: Feb 15, 2006
Posts: 27
|
|
No. Correct answer is : abca1b1c1. First it will execute the static blocks from super class to sub class. Then it will execute the init blocks and the constructors from super class to sub class.
|
Warm Regards,<br />Suresh Kumar<br /> <br />SCJP 5.0<br />SCWCD 1.4
|
 |
Jacky Zhang
Greenhorn
Joined: Sep 17, 2006
Posts: 18
|
|
My study guide says "Static init blocks are executed at class loading time" Based on C extends B, B extends A and new C() is invoked my thought is class C should be loaded first before the call within C's constructor to super() which is B's constructor, then before B's constructor runs, B class is loaded, etc. Thus I would choose CBA... according to my understanding of the class loading time, unless A loads before B, B loads before C, are there any references talks about this? thanks!
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jacky Zhang: My study guide says "Static init blocks are executed at class loading time" ...
It's probably more accurate to say that static blocks are executed when the class is "initialized" -- rather than "loaded." So the question here is really about order of initialization. JLS 12.4 - Initialization of Classes and Interfaces states, "Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables)... Before a class is initialized, its superclass must be initialized..." This particular scenario is illustrated under 12.4.1. In general, initialization must occur from the top downward in an inheritance hierarchy so that the superclass can be used by the subclass.
|
 |
Jacky Zhang
Greenhorn
Joined: Sep 17, 2006
Posts: 18
|
|
|
great! thanks.
|
 |
Paredes Be
Greenhorn
Joined: Sep 19, 2006
Posts: 2
|
|
Remember bro., The static�s blocks are readen first after the constructors. All the static�s blocks must be readen and next all the constructors will be. Look this example and the numbers. class CuerposEstaticos { public static void main (String[] args){ B2 b2 = new B2(); //0 } } class A1 extends X3 { A1() { System.out.println("cA1"); //5 } static { System.out.println("sA1"); //2 } } class B2 extends A1{ B2() { System.out.println("cB2");//6 } static { System.out.println("sB2"); //3 } } class X3 { X3() { System.out.println("cX1"); //4 } static { System.out.println("sX1"); //1 } }
|
 |
 |
|
|
subject: static initialization block
|
|
|