Summary of Usage of this
and super
in Java
Category Programming Technology
this
this
is an object itself, representing the object itself, and can be understood as: a pointer pointing to the object itself.
The usage of this
in Java can be broadly classified into three types:
1. Ordinary Direct Reference
This is straightforward, where this
points to the current object itself.
2. Differentiating Between Formal Parameters and Member Names
Example
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));
}
}
Running Result:
Initial age: 10
Harry's age is 12
Here, age
is the formal parameter of the GetAge
member method, and this.age
is the member variable of the Person
class.
3. Referencing Constructor
This will be discussed together with super
below.
super
super
also has three usages:
1. Ordinary Direct Reference
Similar to this
, super
points to the parent class of the current object, allowing you to reference the parent class's members with super.xxx
.
2. Member Variables or Methods in Subclass with the Same Name as in Parent Class
Example
class Country {
String name;
void value() {
name = "China";
}
}
class City extends Country {
String name;
void value() {
name = "Shanghai";
super.value(); // Call the parent class's method
System.out.println(name);
System.out.println(super.name);
}
public static void main(String[] args) {
City c = new City();
c.value();
}
}
Running Result:
Shanghai
China
Here, both the parent class's method and variable are called. If the parent class's method value()
is not called, the parent class's variable name
would have the default value null
.
3. Referencing Constructor
- super(parameters): Calls a constructor of the parent class (should be the first statement in the constructor).
- this(parameters): Calls another constructor of the same class (should be the first statement in the constructor).
Example
class Person {
public static void prt(String s) {
System.out.println(s);
}
Person() {
prt("Parent class · No-argument constructor: " + "A Person.");
} // Constructor (1)
Person(String name) {
prt("Parent class · Constructor with one parameter: " + "A person's name is " + name);
} // Constructor (2)
}
public class Chinese extends Person {
Chinese() {
super(); // Call the parent class's constructor (1)
prt("Subclass · Calling parent class 'no-argument constructor': " + "A Chinese coder.");
}
Chinese(String name) {
super(name); // Call the parent class's constructor with the same parameter (2)
prt("Subclass · Calling parent class 'constructor with one parameter': " + "his name is " + name);
}
Chinese(String name, int age) {
this(name); // Call the constructor with the same parameter (3)
prt("Subclass: Calling subclass constructor with the same parameter: his age is " + age);
}
public static void main(String[] args) {
Chinese cn = new Chinese();
cn = new Chinese("codersai");
cn = new Chinese("codersai", 18);
}
}
Running Result:
Parent class · No-argument constructor: A Person.
Subclass · Calling parent class 'no-argument constructor': A Chinese coder.
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Calling parent class 'constructor with one parameter': his name is codersai
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Calling parent class 'constructor with one parameter': his name is codersai
Subclass: Calling subclass constructor with the same parameter: his age is 18
From this example, you can see that super
and this
can be used to call the parent class's constructor and other constructors within the same class, respectively.
In the example, the third constructor of the Chinese
class calls the second constructor of the same class, which in turn calls the parent class's constructor. Therefore, the parent class's constructor is called first, followed by the second constructor of the same class, and finally the third constructor is overridden.
Differences and Similarities Between super
and this
super(parameters)
: Calls a constructor of the base class (should be the first statement in the constructor).this(parameters)
: Calls another constructor of the same class (should be the first statement in the constructor).super
: It references the members of the direct parent class (used to access hidden members of the parent class or functions when there is a same member definition in both base and derived classes, e.g.,super.variableName
super.memberFunctionName(arguments)
).this
: It represents the current object name (used to indicate the current object in places where ambiguity may arise; if the formal parameters of a function have the same name as the member variables of the class,this
is used to indicate the member variable name).Calling
super()
must be the first line in the subclass constructor, otherwise, it will not compile. The first statement of each subclass constructor implicitly callssuper()
, and if the parent class does not have a constructor of this form, a compile-time error will occur.super()
andthis()
are similar, the difference being thatsuper()
calls the parent class's constructor from the subclass, whilethis()
calls another method within the same class.Both
super()
andthis()
must be the first statement in the constructor.Although
this
can be used to call one constructor, it cannot call two.this
andsuper
cannot appear together in a constructor becausethis
necessarily calls another constructor, which will also contain asuper
statement. Having identical statements in the same constructor loses their meaning, and the compiler will not pass it.this()
andsuper()
both refer to objects, so they cannot be used in static environments, including static variables, static methods, and static blocks.In essence,
this
is a pointer to the current object, whilesuper
is a Java keyword.
>
Original Link: https://www.cnblogs.com/hasse/p/5023392.html
** Click to Share Notes
-
-
-