This keyword in Java is a reference variable that is used to refer to the current class object inside a method or a constructor. It is mainly used to eliminate the confusion between the class attributes and parameters with the same name.
Table of Contents
[hide]This keyword in Java is used for various purposes. The most common use of this keyword is to differentiate variable of a class and formal parameters of methods and constructors.
Whenever the formal parameter and data member of a class are similar and the JVM gets an amiguity, in that case we should use this keyword.
this. this ()
1. this. is used to differentiate the variableof a class with the formal parameters of methods and constructors while pointing to the current class object. It can be used as :
this.instance variable of current class.
2. this( ) is used to call one constructor within another without creating multiple object of a same class.
this(); //call default constructorthis(param1, param2, ....); //call parametrized constructor
Some other usage of Java this keyword are :
this Keyword is used to refer instance variables of the current class.
this keyword is used to Invoke current class method.
this keyword is used to Invoke current class constructor.
this keyword can be used as a method parameter.
this keyword can be used to Return Current Class Instance
Lets learn more about these usage of this keyword with examples.
Java does not allow to declare a instance variable with the same name again, but it allows instance variable and parameters to have the same name. In this case there is an amiguity, the compiler gets confused between the instance variable and parameter.
Let's take an example where we have instance variables and parameters with the same name and what problem occurs when we use them.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
public void setinfo(String name, int count) {
name=name;
count=count;
}
public void showdata() {
System.out.println("Department name : "+ name);
System.out.println("No of Student : "+ count);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
s1.setinfo("Computer Science", 500);
s1.showdata();
}
}
Department name : nullNo of Student : 0
Now you must be thinking intead of getting the correct values, the output generated is null and 0. During the execution compiler gets confused whether the instance on the left hand side of an operator is an instance variable or a global variable
The solution is "THIS" keyword, it differentiates instance variable from the local variable. Let us see the same program with the solution.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
public void setinfo(String name, int count) {
this.name=name;
this.count=count;
}
public void showdata() {
System.out.println("Department name : "+ name);
System.out.println("No of Student : "+ count);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
s1.setinfo("Computer Science", 500);
s1.showdata();
}
}
Department name : Computer ScienceNo of Student : 500
In real time projects its good to use meaningful names for variables, so its recommended to use same name for instance variable and parameter.
Current Class Method can be invoked by using this keyword, if you do not use this keyword, the compiler will automatically add this keyword when invoking the method.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
public void setinfo(String name, int count) {
this.name=name;
this.count=count;
this.showdata();
}
public void showdata() {
System.out.println("Department name : "+ name);
System.out.println("No of Student : "+ count);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
s1.setinfo("Computer Science", 500);
}
}
Department name : Computer ScienceNo of Student : 500
Current Class Constructor can be invoked by using this keyword, we can either call a default constructor from a parameterized constructor or vice versa. Lets see an example of calling a parameterized constructor from default constructor.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
// default constructor
Department(){
this("Computer Science", 500);
System.out.println("Inside Default Constructor ");
}
//Parameterized constructor
Department(String name, int count){
this.name=name;
this.count=count;
this.showdata();
System.out.println("Inside Parameterized Constructor ");
}
public void showdata() {
System.out.println("Department name : "+ name);
System.out.println("No of Student : "+ count);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
}
}
Department name : Computer ScienceNo of Student : 500 Inside Parameterized Constructor Inside Default Constructor
this keyword is also used for constructor Chaining i.e reuse constructor from another constructor. Lets see an example how you can resuse the constructor.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name, tech;
int count;
Department(String name, int count){
this.name = name;
this.count = count;
}
Department(String name, int count, String tech){
this(name,count); // reusing constructor
this.tech = tech;
}
public void showdata() {
System.out.println(name +" "+count +" "+tech);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department("Computer Science",500);
Department s2 = new Department("Computer Science",500, "Java");
s1.showdata();
s2.showdata();
}
}
Computer Science 500 nullComputer Science 500 Java
Remember that Call to this() must always be the first statement in constructor else it will through you an error.
this keyword can also be passed as a method parameter.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
// default constructor
Department(){
name="Computer Scinece";
count=500;
}
// Method that receives 'this' keyword as parameter
void display(Department obj)
{ System.out.println("Department name :"+name);
System.out.println("No of Student :"+count); }
// Method that returns current class instance
void get()
{ display(this); }
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
s1.get();
}
}
Department name :Computer ScineceNo of Student :500
this keyword is also used to return the current class instance. Let's see this with the help of example.
//Java Program to demonstrate the use of this keyword in Java
class Department {
String name;
int count;
//Default constructor
Department()
{ name="Computer Scinece";
count=500; }
//Method that returns current class instance
Department get()
{ return this; }
//Displaying value of variables name and count
void display()
{ System.out.println("Department name :"+name);
System.out.println("No of Student :"+count);
}
}
public class Codebator {
public static void main (String[] args) {
Department s1 = new Department();
s1.get().display();
}
}
Department name :Computer ScineceNo of Student :500
this keyword might be confusing for begineers, kindly learn only few important usages of this keyword.