this key words :
-In java , this keywords refer to the current object
-To use to differentiate local variable and instance variable
-Example: this.name = name;
-The this keyword in Java is a reference variable that refers to the current object
-It is used within an instance method or a constructor to access members of the current object such as instance variables, methods, and constructors
-Within an instance method or a constructor, this is a reference to the current object the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
why we use this keyword:
-Call current class methods and fields
-To pass an instance of the current class as a parameter
-To differentiate between the local and instance variables
class Constructors{
int phone;
int age;
String name;
Constructors(int phone,int age,String name){
this.phone=phone;
this.age=age;
this.name=name;
}
Constructors(int phone,int age){
this.phone=phone;
this.age=age;
}
Constructors(){
}
void GetData(){
System.out.println("phone:"+phone +"Age:"+age+"Name:"+name);
}
public static void main(String args[]){
Constructors obj=new Constructors(042354,21,"arun");
obj.GetData();
}
}
Top comments (0)