Java Reference
In-Depth Information
3. public String make, model, color;
4.
5. {
6. System.out.println(“Inside Car instance initializer”);
7. color = “Red”;
8. }
9.
10. public Car(String make, String model) {
11. super(4);
12. System.out.println(“Inside Car constructor”);
13. this.make = make;
14. this.model = model;
15. }
16.
17. public String toString() {
18. return make + “ “ + model + “ “ + color;
19. }
20.
21. public static void main(String [] args) {
22. Car ford = new Car(“Ford”, “Mustang”);
23. }
24. }
Running the Car program results in the following sequence of events:
1. The new Car statement executes on line 22 of Car.java , which invokes the constructor
on line 10 of Car .
2. The call to super(4) on line 11 passes control to the Vehicle constructor on line 5,
which displays the message “ Inside Vehicle constructor ”.
3. After the Vehicle constructor returns, the instance initializer in Car on line 5 is
invoked, displaying the message “ Inside Car instance initializer ”.
4. The body of the Car constructor executes last, displaying “ Inside Car constructor ”.
Therefore, the output of main is
Inside Vehicle constructor
Inside Car instance initializer
Inside Car constructor
Search WWH ::




Custom Search