Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static initialization block?????

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks sagar.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic