2

I've been reading an article about constructors in Java and come across the following piece of text:

Execution of instance variable initializers and instance initializers is performed regardless of whether the superclass constructor invocation actually appears as an explicit constructor invocation statement or is provided automatically. (An alternate constructor invocation does not perform this additional implicit execution.)

It's a bit unclear to me about the sentence in parenthesis. Does that mean if we don't specify the alternate constructor to be invoked explcitly, it won't be called implicitly as this(), right?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • possible duplicate of [unnecessary to put super() in constructor?](http://stackoverflow.com/questions/2054022/unnecessary-to-put-super-in-constructor) –  Nov 22 '14 at 07:56

2 Answers2

4

Yes. It's saying that you will always get a call to the super() constructor as the first statement in your constructor (unless you use a this(), but then that will have a super() call as it's first statement - or an alternate call, eventually super() will be called).

Consider what happens in the empty constructor of a hypothetical class like Main with a String value field and three constructors like

private String value;

public Main() {
    this(10); // alternate constructor, super() isn't invoked yet.
}

public Main(int val) {
    this(String.valueOf(val)); // alternate constructor, no super() yet
}

public Main(String str) {
    // super(); // <-- will now implicitly or explicitly super()
    this.value = str;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2
Base s = new Base();

will invoke automatically Super() constructor

But alternate constructor like argument constructor will actually will not call super();

you have explicitly call super() or super(argument) whatever available in the super class

Alternate constructor also includes this() which will call Base() constructor which will not allow super() constructor call and hence initializing will not happen

anshulkatta
  • 2,044
  • 22
  • 30