In this article this keyword in java we give the information about this keyword can be used to refer to the current class instance variable. If there is ambiguity between instance variables and parameter variables, then this keyword solves the ambiguity problem.
this keyword in Java
this keyword can be used to refer to the current class instance variable. If there is ambiguity between instance variables and parameter variables, then this keyword solves the ambiguity problem.
‘this’ is a keyword in Java. Which is a reference variable in java.
The instance variable of the current class is referred to by this keyword.
Usage of this keyword in Java:
- this can be used to refer to the current class instance variable.
- It can be used to implement the current class method (implicitly)
- this() can be used to invoke the constructor of the current class.
- It can be passed as an argument in the method call.
- this can be used to return the instance of the current class from a method.
Here both the instance variable and the local variable are the same in the blow program. That’s why when the value of the instance variable (left) is assigned to the local variable (right), then the compiler does not understand which variable instance and which variable is local. To avoid this problem we should use this keyword.
// without using this keyword
class thisData
{
int p; // instance variable
int q; // instance variable
thisData(int p, int q) // p and q is local variable
{
p = p;
q = q;
}
void getdata()
{
System.out.println(“Value of P : ” + p);
System.out.println(“Value of Q : ” + q);
}
public static void main(String [] args)
{
thisData ob = new thisData (5, 6);
ob.getdata();
}
}
Output:
Value of P: 0
Value of Q: 0
Instance variable and Local variable these two variables have been separated with this keyword in the program.
// using this keyword
class ThisData
{
int p; // instance variable
int q; // instance variable
ThisData(int p, int q) // p and q is local variable
{
this.p = p; // instance variable = local variable
this.q = q; // instance variable = local variable
}
void getdata()
{
System.out.println(“Value of P : ” + p);
System.out.println(“Value of Q : ” + q);
}
public static void main(String [] args)
{
ThisData ob = new ThisData (5, 6);
ob.getdata();
}
}
Output:
Value of P: 5
Value of Q: 6
Using this() without parameter with Constructor
this() is the first statement in the constructor. this() is also used to invoke the constructor of the current class.
Source Code:
class data
{
data()
{
System.out.println(“Default Constructor.”);
}
data(int d)
{
this(); //Default Constructor invoke first
System.out.println(“Parameterized Constructor.”);
System.out.println(“Value of D : ” + d);
}
public static void main(String [] args)
{
data ob = new data(50);
}
}
Output:
Default Constructor.
Parameterized Constructor.
Value of D: 50
Using this() with parameter with Constructor
Source Code:
class DataP
{
DataP ()
{
this(50); //parameterized constructor invoke first
System.out.println(“Default Constructor.”);
}
DataP (int d)
{
System.out.println(“Parameterized Constructor.”);
System.out.println(“Value of D : ” + 50);
}
public static void main(String args[])
{
DataP ob = new DataP ();
}
}
Output:
Parameterized Constructor.
Value of D : 50
Default Constructor.
this keyword using with class method
The ‘this’ keyword is used to invoke the current class method.
Source Code:
class DataC
{
void show(int d)
{
System.out.println(“Value of D : ” + d);
}
void display()
{
this.show(50);
}
public static void main(String args[])
{
DataC ob = new DataC ();
ob.display();
}
}
Output:
Value of D: 50
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining