what is the use of 'this' keyword in java
Answers
"this" is an implicit object whose pointing to current class object for initialized current class data member. When we are going for this.. Suppose I have programmer defined parameterized constructor whose formal parameters name is similar to class data member. Then JVM will go in confusion which variable are current class variable.. So simply we apply" this" keyword... It generate difference between current class data member to formal parameter.
Another explanation:
This is a reference variable used to refer to the current object of the class. It is used with the constructor and used to refer to the current object of the constructor using this keyword.
Example:
USE OF this KEYWORD IN THE PROGRAM BELOW:
class Student {
int rollno;
String name;
float fee;
Student(int rollno, String name,float fee){
this.rollno = rollno;
this.name=name;
this.fee=fee;
}
WITHOUT USING this KEYWORD:
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
Answer:
this is a keyword in Java it can be used inside the method for constructor of a class it works as a reference to the current object whose method and constructor is being invoked this keyword can be used as to refer to any member of the current object from within an instance or a constructor.