Java Reference
In-Depth Information
1.
The appropriate constructor in the child class is invoked.
2.
A call to super() is made, and the flow of control jumps to the appropri-
ate parent class constructor.
3.
When the parent class constructor completes, the flow of control jumps
back to the child class constructor.
4.
Before any statements that follow super() within this child constructor
execute, the instance initializer executes.
5.
Finally, the statements in the child class constructor following the call to
super() execute.
The following InstanceInitDemo program demonstrates this series of events
by instantiating two CDPlayer objects. Study the program carefully and try to
determine the output, which is shown in Figure 7.8.
public class InstanceInitDemo
{
public static void main(String [] args)
{
System.out.println(“Inside main”);
CDPlayer c1 = new CDPlayer(1);
CDPlayer c2 = new CDPlayer(7);
}
}
As with static initializers, I wanted to discuss instance initializers just in case
you run into one someday or a situation comes up where you need to use one.
You will not see or use them everyday, since in almost all situations a con-
structor or other method in the class can accomplish the same result without
needing an instance initializer.
Figure 7.8
The output of the InstanceInitDemo program.
Search WWH ::




Custom Search