Why do this() and super() have to be the first statement in a constructor?

前端 未结 21 1955
北荒
北荒 2020-11-21 23:10

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why?

For example:

public class MyClass {
    publi         


        
21条回答
  •  醉梦人生
    2020-11-21 23:46

    Before you can construct child object your parent object has to be created. As you know when you write class like this:

    public MyClass {
            public MyClass(String someArg) {
                    System.out.println(someArg);
            }
    }
    

    it turns to the next (extend and super are just hidden):

    public MyClass extends Object{
            public MyClass(String someArg) {
                    super();
                    System.out.println(someArg);
            }
    }
    

    First we create an Object and then extend this object to MyClass. We can not create MyClass before the Object. The simple rule is that parent's constructor has to be called before child constructor. But we know that classes can have more that one constructor. Java allow us to choose a constructor which will be called (either it will be super() or super(yourArgs...)). So, when you write super(yourArgs...) you redefine constructor which will be called to create a parent object. You can't execute other methods before super() because the object doesn't exist yet (but after super() an object will be created and you will be able to do anything you want).

    So why then we cannot execute this() after any method? As you know this() is the constructor of the current class. Also we can have different number of constructors in our class and call them like this() or this(yourArgs...). As I said every constructor has hidden method super(). When we write our custom super(yourArgs...) we remove super() with super(yourArgs...). Also when we define this() or this(yourArgs...) we also remove our super() in current constructor because if super() were with this() in the same method, it would create more then one parent object. That is why the same rules imposed for this() method. It just retransmits parent object creation to another child constructor and that constructor calls super() constructor for parent creation. So, the code will be like this in fact:

    public MyClass extends Object{
            public MyClass(int a) {
                    super();
                    System.out.println(a);
            }
            public MyClass(int a, int b) {
                    this(a);
                    System.out.println(b);
            }
    }
    

    As others say you can execute code like this:

    this(a+b);
    

    also you can execute code like this:

    public MyClass(int a, SomeObject someObject) {
        this(someObject.add(a+5));
    }
    

    But you can't execute code like this because your method doesn't exists yet:

    public MyClass extends Object{
        public MyClass(int a) {
    
        }
        public MyClass(int a, int b) {
            this(add(a, b));
        }
        public int add(int a, int b){
            return a+b;
        }
    }
    

    Also you are obliged to have super() constructor in your chain of this() methods. You can't have an object creation like this:

    public MyClass{
            public MyClass(int a) {
                    this(a, 5);
            }
            public MyClass(int a, int b) {
                    this(a);
            }
    }
    

提交回复
热议问题