May 30, 2021 Article blog
The this keyword represents the caller object of the function to which it belongs.
What this keyword does:
Things to watch out for when the this keyword calls other constructors:
Things to watch out for in this keyword:
Things to watch out for when this keyword calls other constructors:
Recommended lessons: In-depth analysis of Java object-oriented, Java basic getting started to framework practices
class Student{
INT ID; // ID card
String name; // Name
// Current Savings: There is a member variable of the same name and a local variable, and the local variable is used inside the method.
Public student (int ID, string name) {// The form parameters of a function are also partial variables.
// Call the construction method of a parameter of this class
// this (name); // If you can call the constructor of the following parameters here, you can resolve the problem of duplicate code.
THIS (); // Call the uncommon structure method of this class.
THIS.ID = ID; //this.id = id; the ID of the local variable gives the ID assignment of the member variable.
System.out.println ("Two Parameters Construction Method Call ...");
}
public Student() {
System.out.println ("" None of the construction method is called ... ");
}
public Student(String name) {
this.name = name;
//System.out.println ("The construction method of a parameter is called ...");
}
}
public class Demo1 {
public static void main(String[] args) {
Student S = New Student (110, "Diga");
System.out.println ("Number:" + S.ID + "Name:" + S.Name);
Student s2 = new Student("W3CSCHOOL");
System.out.println ("Name:" + S2.NAME);
}
}