aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static and Instance Blocks.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static and Instance Blocks.." Watch "Static and Instance Blocks.." New topic
Author

Static and Instance Blocks..

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

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

Joined: Aug 26, 2001
Posts: 7610
When instantiating a object first the instance members are initialized and then the body of the constructor...
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 17, 2002
Posts: 172
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

Joined: Jan 07, 2002
Posts: 2205
I'm not sure what you're asking. main() doesn't call myMethod().
Rob
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 15, 2002
Posts: 94
is there any difference between

and

?


Yet Another SCJP2
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static and Instance Blocks..
 
Similar Threads
Static Initializers.
Doubt
initialization sequence - what is done first?
Help with class initialization
Initializing Sequence