This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super()" Watch "super()" New topic
Author

super()

Usha Vydyanathan
Greenhorn

Joined: Jan 03, 2001
Posts: 17
Hi,
consider this code.
class A
{
A(){System.out.println("In A's Default ctor"} //default ctor
A(int i){System.out.println("Value passed " + i} // Ctor with args
}
class B extends A
{
static int i=5;
B()
{
this(5);
}
B(int i){System.out.println("Value passed " + i)}
}
When I compile and run this code ctor of the base class is being called before calling ctor with argument of the derived class.
My doubt is how compiler is able to insert super() statement even though this(5) is present in the first line of the ctor. I am not able insert both super(arg) and this() in the same ctor because both of them have to be in the first line. Any idea?
regards,
Usha
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If the first line of a constructor is not a call to a constructor in the base class, the default constructor of the base class is automatically invoked.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
shadow liu
Ranch Hand

Joined: Mar 19, 2001
Posts: 33
if constructor find this(or super) in the first line, he will not insert super() automatically, instead he will goto that constructor, until that construct don't have any this/super in first line, at there it will call super automatically.
In your example,
B()
B(int)
super()

Please note, in B(int), he will call super() version instead of super(5), because the default constructor take no args.
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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
 
wood burning stoves
 
subject: super()
 
Similar Threads
Constructor Problem
instantiation?
default constructor must inside the Parent class. why?
why this happend in constructor?
default constructor