this keyword
The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
Other uses of this keyword include :
- It can be used to refer current class instance variable
- It can be used to invoke or initiate current class constructor
- It can be passed as an argument in the method call
- It can be passed as argument in the constructor call
- It can be used to return the current class instance
It can be used to refer current class instance variable :
Example :
1 |
class Student{ int rollno; String name; Student(int rollno, String name){ this.rollno=rollno; this.name=name; } void display(){System.out.println (rollno+" "+name);} } class ThisDemo{ public static void main(String args[]){ Student s1=new Student(111,"Raj"); Student s2=new Student(112,"Sonu"); s1.display(); s2.display(); }} |
—————————————————————————————————————————————-
Output : 100 Raj
101 sonu
—————————————————————————————————————————————-
It can be used to refer current class method :
Example :
public class CurrentMethod {
int i=10;
int j=20;
void add(){
System.out.println(i+j);
}
void show(){
this.add();
System.out.println(“addded”);
}
public static void main(String[] args){
CurrentMethod c = new CurrentMethod ();
c.show();
}
}
—————————————————————————————————————————————-
Output : 30
added
—————————————————————————————————————————————-
It can be used to refer current class constructor :
Example :
1 |
public class Employee {String name, post;int empno, salary;Employee(String name,String post, int salary){this.name=name;this.post=post;this.salary=salary;}Employee(String name,String post, int salary, int empno){this(name, post, salary);this.empno=empno;}void display(){System.<em>out</em>.println(name+" "+post+" "+salary+" "+empno);}public static void main (String[] args){Employee e = new Employee("ram","Advisor",10000);Employee e2 = new Employee("Shyam","Manager",30000,1011);e.display();e2.display();}}<strong>-----------------------------------------------------------------------------------------------</strong>-----------------------------------------Output : ram Advisor 10000 Shyam Manager 30000 1011 |
—————————————————————————————————————————————-
NOTE : call to this must be the first statement in the constructor otherwise we will get compile time error.
1 |
<strong>Example :</strong>public class Employee {String name, post;int empno, salary;Employee(String name,String post, int salary){this.name=name;this.post=post;this.salary=salary;}Employee(String name,String post, int salary, int empno){this.empno=empno;this(name, post, salary);// compile time error}void display(){System.<em>out</em>.println(name+" "+post+" "+salary+" "+empno);}public static void main (String[] args){Employee e = new Employee("ram","Advisor",10000);Employee e2 = new Employee("Shyam","Manager",30000,1011);e.display();e2.display();}}<strong>----------------------------------------------------------------------------------------------------------------------------------------</strong> |
Output : compile time error
—————————————————————————————————————————————