• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static initialization block

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jacky Zhang:
...result: cbaa1b1c1

it this correct? ...


No, but you're close. Have you tried running it?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jacky Zhang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great! thanks.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic