Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static initialization block

 
Jacky Zhang
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Sureshkumar Chinnappan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great! thanks.
 
Paredes Be
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic