Friday, July 30, 2010

Constructors: Hawk.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 3, Question 6

















Trace of this code:
Line 12: start of main() program
Line 13: Hawk() is invoked, and since Hawk() extends Raptor(), control of program goes to Raptor(), i.e. super()
Line 6: Raptor() static initialization block is executed
Line 9: Program is still in Raptor() because it has one more static initialization block
Line 13: Code in Hawk() executes
Line 14: New instance of Hawk() is created, which automatically calls super(), i.e. Raptor()
Line 2: Since Raptor() is invoked, it calls super(), i.e. Bird(), whereby the instance initialization block is executed
Line 3: Bird() constructor is invoked
Line 8: Control of program is returned to Raptor() and the instance initialization block is executed
Line 7: Raptor() constructor is invoked
Line 15: Control of program is returned to Hawk() and the instance initialization block is executed

Final output:

r1 r4 pre b1 b2 r3 r2 hawk

No comments:

Post a Comment