Reputation: 492
The output of this code is 7 20.
Why does 7 print first and 20 is printed after that?
public class Television
{
private int channel = setChannel(7);
public Television(int channel)
{
this.channel = channel;
System.out.print(channel +"");
}
public int setChannel(int channel)
{
this.channel = channel;
System.out.print(channel + "");
return channel;
}
public static void main(String args[])
{
new Television(20);
}
}
Upvotes: 14
Views: 1034
Reputation: 16039
The answer is available in the Java Language Specification:
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this)...
... If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super)...
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
To sum it up, if a constructor does not call another constructor (using this
) and it does not call any superclass constructor (using super
) then the instance variables are initialized before executing the code of the constructor.
Upvotes: 6
Reputation: 96016
When the object is created, its fields are created. You have a class member:
private int channel = setChannel(7);
When you do:
new Television(20);
The field is initialized and setChannel
is called before calling the constructor and 7 gets printed from there.
All fields of the object are created and populated with the provided values (or default values if no value is specified). You can think of that as preparation of the instance. After these members are ready and initialized, the constructor is called.
See the JLS for further and detailed information.
Upvotes: 15
Reputation: 1659
Because that's the order of initialization in Java. In short:
Upvotes: 14
Reputation: 2048
You initialize your field 'channel' outside the class construsctor, so this initialisation is called before it.
This is what appends when you call new Television(20)
:
1/this.channel set to 7 (init step)
2/Calling constructor
3/this.chennel set to 20 (constructor code)
Upvotes: 4
Reputation: 21895
When you create an instance of an object , first you create its data members (i.e. first that line is executed private int channel = setChannel(7);
) and only then the Constructor public Television(int channel) {...}
creates the object.
Upvotes: 3
Reputation: 3467
Because the setChannel in your initialization of the variable channel is called first to init the class, and the method in the constructor after that.
Upvotes: 3
Reputation: 4109
First, the private field is initialized and then the constructor is called.
So the output is 7 20.
In java, the following process occured when instantiate a new object:
Upvotes: 5