This Keyword
In Java, this is a reference variable that refers to the current object
In Java this keyword is used
- To refer current class instance variables
- To invoke current class constructor
- To return the current class instance
- As an argument in method parameter
- To invoke current class method
- As an argument in the constructor call
- Using ‘this’ keyword to refer current class instance variables
If both local variable(also called parameters or formal arguments) and instance variables are same,then we use this keyword to distinguish local variable and instance variable.
Program for when both local and instance variables are same and not using this
public class Item {
int item_id;
String item_name;
float item_price;
// Above variables are called Instance Variables, while below variables inside method
// or parameters are called local variable. Here both instance variables i.e above one
// and local variables i.e inside bracket (parameters) are same
Item(int item_id,String item_name,float item_price){
item_id=item_id;
item_name=item_name;
item_price=item_price;
}
public void display() {
System.out.println(item_id+" "+item_name+" "+item_price);
}
}
public class Item_Main {
public static void main(String args[]){
Item item_1=new Item(111,"Pencil",10f);
Item item_2=new Item(112,"Pen",20f);
item_1.display();
item_2.display();
}
}
Output:
0 null 0.0
0 null 0.0
Program for when both local and instance variables are same and using this
public class Item {
int item_id;
String item_name;
float item_price;
// Above variables are called Instance Variables, while below variables inside method
// or parameters are called local variable. Here both instance variables i.e above one
// and local variables i.e inside bracket (parameters) are same
Item(int item_id,String item_name,float item_price){
this.item_id=item_id;
this.item_name=item_name;
this.item_price=item_price;
}
public void display() {
System.out.println(item_id+" "+item_name+" "+item_price);
}
}
public class Item_Main {
public static void main(String args[]){
Item item_1=new Item(111,"Pencil",10f);
Item item_2=new Item(112,"Pen",20f);
item_1.display();
item_2.display();
}
}
Output:
111 Pencil 10.0
112 Pen 20.0
If local variables(formal arguments) and instance variables are different,there is no need to use this keyword
Program for when both local and instance variables are different
public class Item {
int item_id;
String item_name;
float item_price;
// Above variables are called Instance Variables, while below variables inside method
// or parameters are called local variable. Here both instance variables i.e above one
// and local variables i.e inside bracket (parameters) are different
Item(int id,String name,float price){
this.item_id=id;
this.item_name=name;
this.item_price=price;
}
public void display() {
System.out.println(item_id+" "+item_name+" "+item_price);
}
}
public class Item {
public static void main(String args[]){
Item item_1=new Item(111,"Pencil",10f);
Item item_2=new Item(112,"Pen",20f);
item_1.display();
item_2.display();
}
}
Output:
111 Pencil 10.0
112 Pen 20.0
- Using this() to invoke current class constructor
The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In simple words, it is used for constructor chaining.
Program For calling default constructor from parameterized constructor:
public class Student {
String name;
int rollno;
//Default constructor
Student()
{
System.out.println("Called from default constructor \n");
}
//Parameterized constructor
Student(String name, int rollno)
{
this(); // here this is used to call default constructor
System.out.println("Student Name : " + name + "\n" + "Roll No : " + rollno);
}
public static void main(String[] args)
{
Student object = new Student("Sachin",20);
}
}
Output :
Called from default constructor
Student Name : Sachin
Roll No : 20
Program For calling parameterized constructor from default constructor:
public class Student {
String name;
int rollno;
//Default constructor
Student()
{
this("Sachin", 20);
System.out.println("Called from default constructor \n");
}
//Parameterized constructor
Student(String name, int rollno)
{
this.name = name;
this.rollno = rollno;
System.out.println("Called from inside parameterized constructor");
}
public static void main(String[] args)
{
Student object = new Student();
}
}
Output :
Called from inside parameterized constructor
Called from default constructor
this() constructor call should be used to reuse the constructor from the constructor. This helps in maintaing the chain between the constructors which is called constructor chaining.
P.S : Call to this() of reused constructor must be the first statement in constructor.
Example:
public class Student {
int rollno;
String name,course;
float fee;
Student(int rollno,String name,String course){
this.rollno=rollno;
this.name=name;
this.course=course;
}
Student(int rollno,String name,String course,float fee){
this(rollno,name,course);//reusing constructor i.e constructor chaining
this.fee=fee;
// Call to this(rollno,name,course) of reuse constructor should be called first else it will give compile error
// this(rollno,name,course); // Calling it from here will give compile error
}
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}
public static void main(String args[]){
Student s1=new Student(111,"Sachin","Java");
Student s2=new Student(112,"Nikhil","Android",6000f);
s1.display();
s2.display();
}
}
Output:
111 Sachin Java 0.0
112 Nikhil Android 6000.0
- Using ‘this’ keyword to return the current class instance
We can return this keyword as a statement from the method. In such case,return type of the method must be the class type (non-primitive)
Syntax :
class_name method_name(){
return this;
}
public class Student {
int rollno;
String name;
// Default constructor
Student()
{
rollno = 20;
name = "Sachin";
}
//Displaying value of variables name and rollno
void display()
{
System.out.println("Student Name : " +name +"\n" + "Roll No. : " + rollno);
}
// Method that returns current class instance
Student get()
{
return this;
}
public static void main(String[] args)
{
Student object = new Student();
object.get().display();
}
}
Output:
Student Name : Sachin
Roll No. : 20
- Using ‘this’ keyword as an argument in method parameter
this keyword can be passed as an argument in the method which is mainly used in the event handling.
Program for using ‘this’ keyword as method parameter
public class Student {
int rollno;
String name;
// Default constructor
Student()
{
rollno = 20;
name = "Sachin";
}
// Method that receives 'this' keyword as parameter
void display(Student obj)
{
System.out.println("Student Name : " +obj.name +"\n" + "Roll No. : " + obj.rollno);
}
// Method that returns current class instance
void show()
{
display(this);
}
public static void main(String[] args)
{
Student object = new Student();
object.show();
}
}
Output :
Student Name : Sachin
Roll No. : 20
- Using ‘this’ keyword to invoke current class method
P.S : When you execute the method in your code, directly, it’s called Calling. When someone else executes it for you, it’s Invoking i.e indirectly
The method of the current class is invoked by using the this keyword. If we don’t use the this keyword, compiler automatically adds this keyword while calling the method
Program for using this to invoke current class method
public class ClassTest {
void display()
{
// calling function show()
// Even if we don't use this with show() here compiler will add this automatically
// show();
this.show();
System.out.println("This is display function");
}
void show() {
System.out.println("This is show function");
}
public static void main(String args[]) {
ClassTest c_1 = new ClassTest();
c_1.display();
}
}
Output:
This is show function
This is display function
- Using ‘this’ keyword as an argument in the constructor call
When we have to use one object in multiple classes,we can use the this keyword in the constructor.
public class College {
Student obj;
// Parameterized constructor with object of Student
// as a parameter
College(Student obj)
{
this.obj = obj;
// calling display method of class Student
obj.display();
System.out.println("Welcome To GLS ICT");
}
}
public class Student {
String name="Sachin";
Student()
{
College obj = new College(this);
}
// method to show value of name
void display()
{
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
Student obj = new Student();
}
}
Output :
Hello, Sachin
Welcome To GLS ICT