Note the use of Java this of and super of

  • 2020-06-19 10:26:14
  • OfStack

Java this() and super()

super and this should be noted:

1) The call to super() must be written on line 1 of the subclass constructor, or the compilation will not pass. The first statement of each subclass constructor is an implicit call to super(), and if the superclass does not have this form of constructor, an error will be reported at compile time.

2) super() is similar to this(), except that super calls the superclass constructor from a subclass, and this() calls other methods within the same class.

3) super() and this() should be placed in line 1 of the constructor.

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

5) this and super cannot be in the same constructor at the same time, because this must call other constructors, other constructors must also have the existence of super statement, so the same statement in the same constructor will lose the meaning of the statement, the compiler will not pass.

6) this() and super() both refer to objects, so they cannot be used in the static environment. Includes: static variable,static method, static statement block.

7) Essentially, this is a pointer to the object, while super is an Java keyword.

Why is this or super in line 1?

super this () () was introduced into the current if you want to use the parameters in the constructor or the constructor calls to other constructors or control of data in the superclass constructor is used when, you can only be used in a constructor this () or super 1 (), and the location of the call can only be in the constructor of line 1, in a subclass if you want to call the superclass constructor to initialize the parent class, then use the appropriate parameter to invoke super (), If you call the constructor of the parent class with super() and no other constructor with this(), the default constructor of the parent class will be called, and if the parent class does not have a default constructor, the compiler will report an error.
As follows:


class A{ 
  public A(String name) 
  { 
    System.out.println(name); 
  } 
} 
 
class B extends A{ 
 
  public B(String name) { 
    super(name); 
  } 
   
} 

There is no parameterless constructor constructed in class A and an error will be reported if the constructor in class B is not called super(name).

Suppose we allow this and super to be placed anywhere. Take a look at the following code:


class A{   
  A()   
  {     
    System.out.println("You call super class non-args constructor!");  
  } 
} 
 
 
 class B extends A 
 {    
  B()    
  {   
    // Here, the compiler automatically adds  super();        
    System.out.println("You call subclass constructor!");    
  }          
  B(String n)     
  {         
    super();         
    this();//ERROR: Compile error  
    // It's actually called B(){...} , and in B(){...} The compiler adds it automatically super(); So that's like two calls super(); That is, the parent class is initialized twice. While in instantiation 1 When I have 10 objects, 1 Each constructor can only be called 1 Again, this says this and super Cannot exist at the same time 1 Of the constructor methods. And also because the system is not at number one 1 Line found this() or super() Call, and it will be automatically added super(), If there is no this() and super() In the first 1 Okay, so there's a contradiction. Because there is always 1 a super() In the first 2 On the other. So the program cannot compile!!   
  } 
 } 

That is to say, you must be placed in the constructor of line 1 super or this constructor, otherwise the compiler will automatically put an empty parameter super constructor, other constructors can also call super or this, call into a recursive structure chain, the end result is the parent class constructor (there may be multiple superclass constructor) before a subclass constructor performs all the time, a recursive call the superclass constructor. The constructor for the current class cannot be executed. No object can be instantiated, and the class becomes a nonentity.

On the other hand, a subclass inherits the properties and methods of the superclass. If the subclass does not initialize the members of the superclass first, the subclass cannot use it, because it is not allowed to call uninitialized members in java. This is done sequentially in the constructor, meaning that the parent class must be initialized at line 1. super does this directly. This() also does this by calling other constructors in this class.

Therefore, this() or super() must be placed on line 1.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: