Hi Usha...,
I have one simple articel about constructor (i forgot where), but I still have the paper, it was realy help me understanding the consept of basic order initialization, calling super & calling this in constructor.
I hope it will help you too...
following code:
1: class A {
2: public A() {
3: System.out.println("AAA");
4: }
5: {
6: System.out.println("456");
7: }
8: }
9:
10: public class B extends A {
11: B() {
12: this(12);
13: System.out.println("BBB");
14: }
15: B(int x) {
16: System.out.println("CCC");
17: }
18: {
19: System.out.println("123");
20: }
21: public static void main(
String[] args) {
22: new B();
23: }
24: }
The output is:
456
AAA
123
CCC
BBB
The first statement in any
Java constructor will always be one of the following:
1. A call to another constructor in the same class using the "this();" notation. For example, this(parm1, parm2, parm3);
2. A call to a constructor in the super class using the "super();" notation. For example, super(parm1, parm2, parm3);
3. Neither of the above two in which case the compiler invisibly inserts a call to the superclass no-arg constructor as the first statement: super();
From the above three cases it should be clear that if some class B extends another class, A, calling B's constructor will result in one of A's constructors being executed in its entirety before the second statement in B is ever executed.
Once a class' base class constructor has completed executing, any instance initializers defined by the class are executed, in the order in which they are defined. After all instance initializers have executed the constructor body finally executes.
Using the example code above, the following events occur:
1. B's no-arg constructor is invoked from main().
2. The B() constructor makes a call to another constructor (B(int)) in its class using the "this()" notation: this(12);.
3. The B(int) constructor does not call another constructor in the same class nor does it explicitly call a constructor in the base class. Therefore, the compiler IMPLICITLY inserts a call to A's no-arg constructor as the first statement in B(int).
4. The A() constructor does not call another constructor in the same class nor does it explicitly call a constructor in the base class (java.lang.Object). Therefore, the compiler IMPLICITLY inserts a call to java.lang.Object's no-arg constructor as the first statement in A().
5. After java.lang.Object's no-arg constructor returns, all instance initializers in A are then executed. This results in the string "456" being printed.
6. After A's instance initializer completes the A() constructor body executes, printing the string, "AAA".
7. Once A's constructor completes control is returned to the B(int) constructor. But before the body of that constructor executes B's instance initializers must be run. This results in the string, "123" being printed.
8. After B's instance initializer completes, the B(int) constructor body executes, printing the string, "CCC".
9. Finally, control returns back to B's no-arg constructor which prints, "BBB".
hope that helps
stevie