• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic