Suzan Cioc
Suzan Cioc

Reputation: 30097

Is calling super() constructor should be the very first line of the constructor?

Is calling super() constructor should be the very first line of the constructor? If so then why? Why can't I do some simple limited calculations before constructor call, for example, constructor parameters calculation?

I found a situation with inner class constructors which can be called with closure specification:

class A {
    class Inner1 {
        Inner1() {
            // do something
        }
    }
}

class B {
    A a1 = new A();
    A a2 = new A();

    class Inner2 extends A.Inner1 {
         Inner2(boolean sel) {
             (sel?a1:a2).super();
         }
    }

}

This case shows we can want to select enclosing instance for a base class constructor. Why selection logic should be so limited? Why one can't write something like this

if( sel ) {  
    a1.super();
}
else {
    a2.super();
}

ADDITION

By my question I mean that the limitation could be like in the following case:

public class Base {

private final String content;

public Base(String content) {
    this.content = content;
}

public String getContent() {
    return content;
}

}


public class Derived extends Base {

public Derived(String content) {
    super(String.format("Current value of content is %s.", getContent()));
}


}

In latter case I:

1) Fulfilling the requirement of super() to be in the first line

2) Violating the order of construction

3) Get a compiler error "Cannot refer to an instance method while explicitly invoking a constructor"

So, why we can't abolish "first line requirement" and rely only on errors like the last one?

Upvotes: 0

Views: 2873

Answers (2)

Perception
Perception

Reputation: 80603

Yes, a call to super() is required as the very first call in a constructor.

So much so that if you leave it out the compiler will (attempt to) insert the call for you. To understand the why, you would need to understand the philosophies of Java's designers. Gosling has always been in the camp of computer scientists that believe that accessing partially initialized objects is one of the bigger sources of bugs in computer programs. And as such he designed a strict initialization hierarchy that would help to alleviate this problem. Wether you agree with the philosophy is moot - but its important to realize that its as important a concept in Java as for example, references vs pointers, or real, bounded arrays. It should be noted that even languages like Objective C that allow you to invoke initialization at any time, go to great length to enforce initialization chaining, except that they need to do so via convention, as opposed to strict language rules.

I'm not sure what you were trying to illustrate in your example - but after years of development with Java I doubt you will find many cases where you really need to perform logic before invoking super.

Upvotes: 3

ejb_guy
ejb_guy

Reputation: 1125

Constructor calls are chained with super of every class in hierarchy being invoked before constructor of that class is invoked. As all classes in Java inherited from object class, so constructor of Object class is invoked first for every class with reason being that memory allocation for object is done by constructor of Object class

Upvotes: 0

Related Questions