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

前端 未结 21 1946
北荒
北荒 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:27

    Actually, super() is the first statement of a constructor because to make sure its superclass is fully-formed before the subclass being constructed. Even if you don't have super() in your first statement, the compiler will add it for you!

提交回复
热议问题