This keyword has the following uses: 1.used to refer to the current class instance variable
class Student{ int id; String name; student(int id,String name){ this.id = id; this.name = name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); s1.display(); s2.display(); } }
here the parameter and instance variable are the same, so we use this 2.used file to call the current constructor class
class Student{ int id; String name; Student (){System.out.println("default constructor is invoked");} Student(int id,String name){ this ();
3.this keyword can be used to call a method of the current class (implicitly)
4.This can be passed an argument in a method call
5.This can be passed an argument in a constructor call
6. It can also be used to return the current instance of the class.
source share