Java This Keyword
In Java language, 'this' is the keyword which is a reference variable that refers to the current object. The 'this' keyword used inside any method to refer to the current object.
- It is used to refer to the instance variable of the current class.
- It is used to invoke the method of the current class.
- It is used to invoke the constructor of the current class.
Syntax:
this
Example:-
See the example below without using this keyword.
class Student{ int rollno; String name; Student(int rollno,String name) { rollno=rollno; name=name; } void showdata() { System.out.println(rollno+" "+name); } } public class Withoutthiskeyword{ public static void main(String args[]) { Student s1=new Student(111,"Sundarajan"); Student s2=new Student(112,"Backiyalakshmi"); s1.showdata(); s2.showdata(); } }
Example:-
See the example below using this keyword.
class Student{ int rollno; String name; Student(int rollno,String name) { this.rollno=rollno; this.name=name; } void showdata() { System.out.println(rollno+" "+name); } } public class Thiskeyword{ public static void main(String args[]) { Student s1=new Student(111,"Sundarajan"); Student s2=new Student(112,"Backiyalakshmi"); s1.showdata(); s2.showdata(); } }
Quickly Find What You Are Looking For
Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.