Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
This Keyword in Java with Examples
In this tutorial, we'll learn about this
keyword in Java and how it is used to refer to the current object. We'll see examples to help us understand and use this important concept in our Java programming.
The this
keyword in Java is a reference to the current object of a class. It is used to refer to the current object's variables and methods. The this
keyword is commonly used in constructors and setter methods to refer to the current object, but it can also be used in any other method or block of code.
The this keyword can be used to refer to the current object's variables and methods. For example, consider the following class:
class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
In this example, the this
keyword is used in the constructor and the setter methods to refer to the current object's variables. It is used to distinguish between the class's variables and the method's parameters. Without the this
keyword, the code would not be able to tell which variable to assign the value to.
Various Examples of this Keyword in Java:
The this keyword
can be used in different scenarios, let's discuss them one by one with the help of examples:
1. This Keyword in Constructor:
The this
keyword is commonly used in constructors to refer to the current object's variables. This allows you to distinguish between the class's variables and the constructor's parameters.
class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
}
In this example, the this
keyword is used in the constructor to refer to the current object's variables name
and age
. Without the this
keyword, the code would not be able to tell which variable to assign the value to.
2. This Keyword in Setter Method:
The this
keyword is also commonly used in setter methods to refer to the current object's variables. This allows you to distinguish between the class's variables and the setter method's parameters.
class Employee {
private String name;
public void setName(String name) {
this.name = name;
}
}
In this example, the this
keyword is used in the setter method to refer to the current object's variable name
. Without the this keyword, the code would not be able to tell which variable to assign the value to.
3. This keyword in Method:
The this
keyword can also be used in any method to refer to the current object's variables and methods. For example:
class Employee {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public void updateInfo(String name, int age) {
setName(name);
setAge(age);
this.printInfo();
}
}
In this example, the this
keyword is used in the updateInfo
method to call the printInfo
method on the current object. This allows the updateInfo
method to print the updated information after setting the new name and age.
4. This keyword in Anonymous Inner Class:
The this
keyword can also be used in an anonymous inner class to refer to the current object. For example:
class Outer {
private int x = 100;
public void makeInner() {
Inner in = new Inner(); in .seeOuter();
}
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + Outer.this);
}
}
}
In this example, the this
keyword is used in the inner class to refer to the current object of the inner class, and the Outer.this
is used to refer to the current object of the outer class.
5. This keyword in Static Context:
The this
keyword cannot be used in a static context to refer to the current object, as static methods do not have a reference to an object. Instead, the this
keyword can be used to refer to the current class. For example,
class Employee {
private static int count;
public static void incrementCount() {
this.count++; //compile time error
}
}
In this example, the this
keyword is used to refer to the current class, but it will give a compile-time error, as the this
keyword cannot be used in a static context.
In conclusion, the this
keyword in Java is a reference to the current object of a class. It is used to refer to the current object's variables and methods, especially in constructors and setter methods. But it can also be used in different scenarios like in any method, in Anonymous Inner Class, even in static context as well. Understanding and using the this
keyword correctly can help us create more clear and organized code in our Java programming.