The difference between super() and this()

The difference between super() and this()

1) The call to super() must be written in the first line of the subclass constructor, otherwise the compilation will fail. The first statement of each subclass constructor implicitly calls super(). If the parent class does not have a constructor in this form, an error will be reported during compilation.

2) super() is similar to this(), the difference is that super calls the constructor of the parent class from the subclass, and this() calls other methods in the same class.

3) Both super() and this() need to be placed in the first line of the constructor.

4) Although you can call one constructor with this, you cannot call two.

5) This and super cannot appear in a constructor at the same time, because this will inevitably call other constructors, and other constructors must also have super statements, so if there are the same statements in the same constructor, just Losing the meaning of the statement, the compiler will not pass.

6) Both this() and super() refer to objects, so neither can be used in a static environment. Including: static variables, static methods, static statement blocks.

7) Essentially, this is a pointer to this object, whereas super is a Java keyword.
Measure
Measure

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325633337&siteId=291194637