Rob
SCJP 1.4
So 1 prints last because by the time the constructor body is executed, all the instance's variables have been intialized.
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
Rob
SCJP 1.4
Rob
SCJP 1.4
Rob
SCJP 1.4
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![]()
but in this example ....
why is the statement int y=am(4) not being run.
Rob
SCJP 1.4
Yet Another SCJP2
Rob
SCJP 1.4
Right! We're on it! Let's get to work tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
|