Summary of this and super usage in Java

this

this is an object of itself, representing the object itself, which can be understood as: a pointer to the object itself.

The usage of this can be roughly divided into 3 types in Java:

1. Ordinary direct references

Needless to say, this is equivalent to pointing to the current object itself.

2. Participating members have the same name and use this to distinguish:

Examples

class Person {
     private int age = 10;
     public Person () {
     System.out.println ("Initial age:" + age);
}
     public int GetAge (int age) {
         this.age = age;
         return this.age;
     }
}
 
public class test1 {
     public static void main (String [] args) {
         Person Harry = new Person ();
         System.out.println ("Harry's age is" + Harry.GetAge (12));
     }
}

operation result:

Initialization age: 10
Harry's age is 12

As you can see, here age is the formal parameter of the GetAge member method, and this.age is a member variable of the Person class.

3. Reference constructor

Let's talk about this with super, see below.

super

Super can be understood as a pointer to its super (parent) class object, and this super class refers to the parent class closest to itself.

There are also three uses of super:

1. Ordinary direct references

Similar to this, super is equivalent to pointing to the parent class of the current object, so you can use super.xxx to refer to members of the parent class.

2. A member variable or method in the subclass has the same name as a member variable or method in the parent class

Examples

class Country {
     String name;
     void value () {
        name = "China";
     }
}
  
class City extends Country {
     String name;
     void value () {
     name = "Shanghai";
     super.value (); // Call the parent method
     System.out.println (name);
     System.out.println (super.name);
     }
  
     public static void main (String [] args) {
        City c = new City ();
        c.value ();
        }
}

operation result:

Shanghai
China

As you can see, both the methods of the parent class and the variables of the parent class are called here. If the parent class method value () is not called, and only the parent class variable name is called, the parent class name value is null by default.

3. Reference constructor

super (parameter): call one of the constructors in the parent class (should be the first statement in the constructor).
this (parameter): call another form of constructor in this class (should be the first statement in the constructor).

Examples

class Person {
     public static void prt (String s) {
        System.out.println (s);
     }
   
     Person () {
        prt ("Parent · No Parameter Constructor:" + "A Person.");
     } // Construction method (1)
    
     Person (String name) {
        prt ("Parent class · Constructor with one parameter:" + "A person's name is" + name);
     } // Construction method (2)
}
    
public class Chinese extends Person {
     Chinese () {
        super (); // Call the parent class constructor (1)
        prt ("Subclass · Call parent class" No parameter constructor ":" + "A Chinese coder.");
     }
    
     Chinese (String name) {
        super (name); // Call the constructor of the parent class with the same formal parameters (2)
        prt ("Subclass · Call parent class" with one parameter constructor ":" + "his name is" + name);
     }
    
     Chinese (String name, int age) {
        this (name); // Call the constructor with the same formal parameters (3)
        prt ("Subclass: call subclass constructor with the same formal parameters: his age is" + age);
     }
    
     public static void main (String [] args) {
        Chinese cn = new Chinese ();
        cn = new Chinese ("codersai");
        cn = new Chinese ("codersai", 18);
     }
}

operation result:

Parent class · No-parameter constructor: A Person.
Subclass · Call parent class "parameterless constructor": A Chinese coder.
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Call parent class "constructor with one parameter": his name is codersai
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Call parent class "constructor with one parameter": his name is codersai
Subclass: call the constructor of the subclass with the same parameters: his age is 18

As you can see from this example, you can use super and this to call the constructor of the parent class and other forms of constructors in this class, respectively.

In the example, the third constructor of the Chinese class calls the second constructor of this class, and the second constructor calls the parent class, so the constructor of the parent class must be called first, and then the first Two, and finally the third method of rewriting.

Similarities and differences between super and this

super (parameter): call a constructor in the base class (should be the first statement in the constructor)
this (parameter): call another formed constructor in this class (should be the first statement in the constructor)
super: 引用 It refers to members in the immediate parent of the current object (used to access member data or functions in the hidden parent in the direct parent, when the base class and the derived class have the same member definitions, such as: super. Variable name super .Member letter data name (actual parameter)
this: It represents the current object name (where ambiguousness is prone to occur in the program, this should be used to indicate the current object; if the member data in the shape participation class of the function has the same name, then this is used to indicate the member variable name)
The call to super () must be written on the first line of the subclass constructor, otherwise the compilation will fail. The first statement of the constructor of each subclass is an implicit call to super (). If the parent class does not have this form of constructor, an error will be reported when compiling.
super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () calls other methods within the same class.
Both super () and this () need to be placed on the first line inside the constructor.
Although you can call one constructor with this, you cannot call both.
this and super cannot appear in one constructor at the same time, because this will necessarily call other constructors, and other constructors will also have super statements, so if you have the same statement in the same constructor, you will lose it. The meaning of the statement will not be passed by the compiler.
This () and super () both refer to objects, so neither can be used in a static environment. Including: static variables, static methods, static statement blocks.
Essentially, this is a pointer to this object, whereas super is a Java keyword.

Author: