this: appears to be used to distinguish between local variables and member variables with the same name.
This is therepresentative of this kind of object, in the end is representing which one?
This represents a reference to the object to which the function is located.
Simply put, which object is the one that is calling this function, this represents which object.
Class children{
private String name;
private int age;
Children (String name) {
This.name=name;
}
Public boolean compare (person p) {
return this.age==p.age;
}
public static void Main (string[] str) {
Children c1=new Children ("Allen");//this representative C1
Children c2=new Children ("Rose");//this representative C2
C1.compare (C2);
}
}
This applies: When you define a function in a class, it is used inside the function to use this to represent the object when the function is called.
This type of object is used internally for this kind of function .
The This keyword is called between constructors:
Class children{
private String name;
private int age;
Children (String name) {
This.name=name;
}
Children (String Name,int age) {
This (name);
This.age=age;
}
}
This statement: used to call each other between constructors. You can only define the first row in the constructor, because initialization is performed first.
The This keyword in Java