wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static initialization block????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static initialization block?????" Watch "Static initialization block?????" New topic
Author

Static initialization block?????

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

Static initialization blocks are always executed before the excution of any method or any other member of class,and also static blocks are never inherited to the subclass.So here when i am calling class B's constructor so it should have executed class B's static block first and then class A's.But it is executing class A's first then class B's........

<blockquote>code:
<pre name="code" class="core">
class A
{
static
{
System.out.println(1);
}
}
class B extends A
{
static
{
System.out.println(2);
}
B()
{
System.out.println(3);
}
}
class SIB
{
public static void main(String punnu[])
{
B b1 = new B();
A a1 = new A();
}
}
</pre>
</blockquote>
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

In general , when there are more than one static blocks define, the order of execution depends upon the order in which they defined, i.e , the first defined blocks execute then second block in order of declaration ..

So i thought when you are loading class B, its executes it parent class (A) , static block first then its derived class (B)..

See the JLS for details .

I may be wring i n explaining somewhere, So any fellow ranchers , please correct me .


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

i got your point sagar.But i think that execution is sequential when there are more than one static blocks in same class.but here the thing is that , static blocks and constructors are never inherited to subclass and since here i am having two classes with, each with one static block.Since i am first creating a object of class B so first class B is invoked and its static block should have executed.but the output is 1 2 3. So i am not getting this concept.......
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by PUNEET MITTAL
but the output is 1 2 3

yes it is correct, since object of class B cannot be constructed until class A completes.
So A's static block goes first and hence the output.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by PUNEET MITTAL:
i got your point sagar.But i think that execution is sequential when there are more than one static blocks in same class.but here the thing is that , static blocks and constructors are never inherited to subclass and since here i am having two classes with, each with one static block.Since i am first creating a object of class B so first class B is invoked and its static block should have executed.but the output is 1 2 3. So i am not getting this concept.......


This thing is beautifully explain in Java Language Specification !

this is a part what they say about it :

Before a class is initialized, its superclasses are initialized, if they have not previously been initialized.

Thus, the test program:

class Super {
static { System.out.print("Super "); }
}
class One {
static { System.out.print("One "); }
}
class Two extends Super {
static { System.out.print("Two "); }
}
class Test {
public static void main(String[] args) {
One o = null;
Two t = new Two();
System.out.println((Object)o == (Object)t);
}
}

prints:

Super Two false

The class One is never initialized, because it not used actively and therefore is never linked to. The class Two is initialized only after its superclass Super has been initialized.


Read this link for details !!

Hope this help .
[ July 16, 2008: Message edited by: Sagar Rohankar ]
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

so we can say that it is something like first all the super classes will be loaded into the base class and then the base class's object will be created.So then the static blocks will be in sequence like super class static block will be at top of the order and so the execution occurs accordingly.
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

ok thanks sagar.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Right, As per my thinking and what JLS suggest !, The class first loaded its super classes and same thing while creating object , all the super class constructor gets called, first.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static initialization block?????
 
Similar Threads
initialization sequence
doubt on object
Run a ProC or C file from Java
Anonymous Inner Class
StringBuffer / StringBuilder methods