2

I am studying for the Java SE 7 exam and I am looking at sample questions. I cannot seem to figure out why the following program returns in the order x y c g.

I understand why the x is run first, because it is a static initialisation block but can someone please explain why y is run before c and g?

public class Triangle {
Triangle() {
    System.out.print("c ");
}

{
    System.out.print("y ");
}

public static void main(String[] args) {
    new Triangle().go();
}

void go() {
    System.out.print("g ");
}

static {
    System.out.print("x ");
}
}
eis
  • 51,991
  • 13
  • 150
  • 199
theBigOzzy
  • 77
  • 1
  • 1
  • 11

4 Answers4

4
{
    System.out.print("y ");
}

is an instance initialisation block and is run before the constructor. So the code is equivalent to:

Triangle() {
    System.out.print("y "); //initialisation block
    System.out.print("c ");
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • so can you have more then one instance of an initialsation block in a class? if so do these run in a specific order? – theBigOzzy Aug 01 '13 at 11:20
  • 1
    @theBigOzzy yes you can and they run in the program order (i.e. in the order they appear in your code). It is defined in [JLS #12.5](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5), step 4: *Execute the instance initializers and instance variable initializers for this class [...] in the left-to-right order in which they appear textually in the source code for the class* – assylias Aug 01 '13 at 11:20
4

Order will be static block:

static {
   System.out.print("x ");
}

Then instance initializer :

{
  System.out.print("y ");
}

which is implicitly :

Triangle() {
   System.out.print("y ");
   System.out.print("c ");
}

Read:

  1. Instance Initializers
  2. Creation of New Class Instances.
  3. Oracle tutorial.
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

the "y" is inside a block statement, not in a method. Therefore the statements inside the block get executed when an instance of Triangle is created, even before the constructor.

marlonp33
  • 139
  • 2
  • 11
-1

In memory first IIB and SIB get executed then your main function loaded into stack that's why you getting this output. As it name suggest IIB (intilised instance block) this is very important static is like free sailors which are floating on sea can be used by any class before main method get executed.

public class TestFirst

{
System.out.println(" From IIB");

}
static
{
    System.out.println("SIB ");
}

     public static void main(String   [] args){

System.out.println(" TestFirst ");

}

}
Sheel
  • 847
  • 2
  • 8
  • 20
  • ..."static is like free sailors which are floating on sea"? what? – eis Aug 01 '13 at 11:27
  • @Envious. Totally wrong. `static` blocks are executed after the class is loaded, not before it. And constructors are only executed, when an instance of class is created, not when the class is loaded. – Rohit Jain Aug 01 '13 at 11:30
  • [@eis](http://stackoverflow.com/users/365237/eis) there are many kind of static are there,do not mind if I have not clearly mentioned in reference to static.There are so many kind of static are there like variable,method can used by anyone means any Class that's why I have called free sailors and sea means our memory for pro-gramme – Sheel Aug 01 '13 at 11:36
  • [@RohitJain](http://stackoverflow.com/users/1679863/rohit-jain) yea you are right.I never wanted to say that may be I have mis-formated so I have deleted my comment because nothing can be loaded before loading the class,what I think when we invoke command :java test. then by Bootstrap class loader our class is loaded.If I am wrong correct me please. – Sheel Aug 01 '13 at 11:46
  • java test for only example I have taken. – Sheel Aug 01 '13 at 11:48
  • @eis I am new to Stackoverflow.I am trying to provide solution of my best but never get credit.Someone has answered same just before me 10second he got full prize.Dont know what is mess in Stackoverflow. – Sheel Aug 01 '13 at 14:50
  • @Envious look at the quality of your posting. Even if you would've been 10 seconds ahead, I don't think you would've got upvotes, since it is not easy to understand what you are trying to tell, code example is messy etc. Focus on quality, not speed. – eis Aug 01 '13 at 15:06
  • By looking at your other posts, it seems you would benefit from reading these and trying to improve your postings based on those: http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx http://blog.stackoverflow.com/2010/10/asking-better-questions/ http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question http://stackoverflow.com/questions/how-to-answer - especially Jon Skeets blog post is a good read. – eis Aug 01 '13 at 15:13
  • @eis thanks for your suggestion but due to my bad post I can not able to ask question but I can provide answers so I will try to improve my rating on behalf of answers. – Sheel Aug 02 '13 at 04:29
  • @Envious I hope you noticed that 2 of links I provided were related to answers and 2 for questions, so they are of benefit to read also for answering. – eis Aug 02 '13 at 06:40
  • @eis yea I have noticed.Thanks – Sheel Aug 02 '13 at 07:52