This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static and Instance Blocks..

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the following program print?

Answer : It will print a b c 2 3 4 1
As I feel, in this case first the static blocks are printed IN THE ORDER THEY ARE DEFINED.
Second the Instance Blocks are defined in the ORDER THEY ARE DEFINED.
Ok I agree with a b c;but can any one tell me why is "1" being printed last.it should a b c 1 2 3 4 as one is being defined first..
Please explain.
Sonir
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor body of a method does not actually start running until:
1 The superclass constructor has completed
2 all instance variable declaration initializers and instance intializers have completed.
So 1 prints last because by the time the constructor body is executed, all the instance's variables have been intialized.
Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So 1 prints last because by the time the constructor body is executed, all the instance's variables have been intialized.


Im confused on this, I can see how the static method would execute first to print "a b c", still though, the main does instantiate the InitTest which should print "1 2 3 4" not one last in my view. Im wrong I know it, can someone explain why?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When instantiating a object first the instance members are initialized and then the body of the constructor...
HIH
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
The constructor body of a method does not actually start running until:
1 The superclass constructor has completed
2 all instance variable declaration initializers and instance intializers have completed.
So 1 prints last because by the time the constructor body is executed, all the instance's variables have been intialized.
Rob

The constructor runs LAST, after the variable and instance intializers for the new object have run.

Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys,
private static String myMethod(String s)
{
System.out.println(s)
}
So your saying the constructor runs last, and myMethod will be executed first in main(), even w/o creation of the object?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're asking. main() doesn't call myMethod().
Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s3 = myMethod("2")
{
s1 = myMethod("3")
}
Rob, so your saying that this call is just being invoked even though the call is outside the main(). I think maybe I need someone to actually walk me through the steps of how "2 3 4 1" get printed and in this order. Now Im confused
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, by the numbers...

Let's assume the class has already been loaded, so a b c has already printed out.
First, in main(), we actually create an instance of the class:
InitTest it = new InitTest();
So, we're instantiating a new InitTest.
FIRST, in InitTest, the no-arg constructor calls its superclass constructor (implicitly).
public InitTest()
{
super(); //FIRST
...
THEN, after the superclass has been initialized, control returns to just after the super() call. NEXT, all instance initializers run, in the order they appear in the class.
String s3 = myMethod("2"); //SECOND
Then this instance initializer:
{
s1 = myMethod("3"); //THIRD
}

Then another variable initializer:
String s4 = myMethod("4"); //FOURTH
NOW all instance initializers have run, so we return to the constructor and start executing at the first statement:
s1 = myMethod("1");

And that's how it works.

Rob
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in this example ....
why is the statement int y=am(4) not being run.
---------------------------------------
public class Test {
static {System.out.println("first");}
int y = am(4);// not being run
public static void main(String args[])
{
System.out.println("main-method");
}
static {System.out.println("second");}
static {int x = am(3);}

static int am(int m)
{System.out.println(m);return m;}
}
----------------------------------------
output:
first
second
3
main-method
----------------------------------------
Originally posted by Rob Ross:
Ok, by the numbers...

Let's assume the class has already been loaded, so a b c has already printed out.
First, in main(), we actually create an instance of the class:
InitTest it = new InitTest();
So, we're instantiating a new InitTest.
FIRST, in InitTest, the no-arg constructor calls its superclass constructor (implicitly).
public InitTest()
{
super(); //FIRST
...
THEN, after the superclass has been initialized, control returns to just after the super() call. NEXT, all instance initializers run, in the order they appear in the class.
String s3 = myMethod("2"); //SECOND
Then this instance initializer:
{
s1 = myMethod("3"); //THIRD
}

Then another variable initializer:
String s4 = myMethod("4"); //FOURTH
NOW all instance initializers have run, so we return to the constructor and start executing at the first statement:
s1 = myMethod("1");

And that's how it works.

Rob

not being run
[ February 01, 2002: Message edited by: mark stone ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in this example ....
why is the statement int y=am(4) not being run.

In your code you are never creating an instance of the class, so the instance variable is never initialized.

Rob
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any difference between

and

?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, yes, but the end result is the same. In the first case you have a static variable declaration, followed by a static initializer block, in which you have an assignment statement.
In the second case you have a static variable declaration with an initializer.
Rob
[ February 01, 2002: Message edited by: Rob Ross ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic