Have you ever thought about how to access the currently executing object of a class in Java? Don't worry if you don't know about it,
because we will discuss just that and a lot more relevant in this tutorial.
Java language provides us a keyword named this, which can be used to:
Access the currently executing object of a class.
Access the instance variables of the currently executing object.
Call the methods associated with the currently executing object.
Call the constructor of a class from within another constructor of the same class.
To resolve the shadowing issue, when a local variable has the same name as an instance variable.
Let us explain each of the above-described uses of this keyword with examples.
Accessing the currently executing object by using this keyword.
We can access the currently executing object by using this keyword, which in this case, is also known as this reference.
// Example of this keyword in Java
class A
{
public void message()
{
System.out.println("Hello from A");
System.out.println(this);
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}
Output
Hello from A
A@70dea4e
Program Analysis
We have first created an object of class A and then we have called the message() method on this object.
Because this refers to the currently executing object, therefore we have got the hexadecimal representation of the object's address
in the memory by printing this.
Accessing the instance variables through this keyword.
If we can access an object by using this keyword, then we can also easily access the instance variables associated with an object by using this keyword. Let's see how.
// Example of this keyword in Java
class A
{
int a=10;
public void message()
{
System.out.println("Hello from A");
System.out.println(this.a);
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}
Output -
Hello from A
10
Because this refers to the currently executing object, hence, by using this keyword,
we have accessed the value of an instance variable, a, which is associated with the currently executing object.
Advertisement
Calling a method using this keyword.
We can access the currently executing object of a class by using this keyword, which also implies that
we can even call the methods associated with the currently executing object.
// Example of this keyword in Java
class A
{
int a=10;
public void message()
{
System.out.println("Hello from A");
this.hello();
}
public void hello()
{
System.out.println("Bonjour");
System.out.println("Hello");
System.out.println("Namaste");
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}
Output is
Hello from A
Bonjour
Hello
Namaste
In the preceding code, we have called the hello() method of the currently executing object by using this keyword.
Using this keyword to call a constructor from another constructor of the same class.
Using this keyword, we can even call a constructor from within another constructor of the same class.
class A
{
int a;
//constructor1
A()
{
this(10); //calling another constructor using this keyword.
}
//constructor2
A(int x)
{
a=x;
}
public static void main(String... ar)
{
A ob = new A();
System.out.println(ob.a);
}
}
Output-
10
Program Analysis
In the last code, class A is defined with two constructors.
The first constructor, which that doesn't take any value in its parameters is called to create an object of class A.
From within the first constructor, we have called the second constructor of class A(using this keyword), and have also passed the required int
parameter to the second constructor while calling it.
Note: Using this keyword, you cannot call a constructor of a class from within a method, and doing so will result in a compile error.
Shadowing issue without using this keyword.
Shadowing issue is caused when a local variable overshadows an instance variable by having the same name as the instance variable in question.
class A
{
int a; //instance variable, a
public void putValue(int a) //local variable, a
{
a=a;
}
public static void main(String... ar)
{
A ob = new A();
//Passing 10 to putValue() method, for 10 to be stored in instance variable a
ob.putValue(10);
//Accessing the value of instance variable, a
System.out.println("Value in a : "+ ob.a);
}
}
Output is
Value in a : 0
Program Analysis
We have created a class with a method putValue(), to initialize an instance variable, a.
The putValue() method takes an int value in its local variable named, a, similar to the name of the instance variable a.
By calling the putValue(10) method, the value 10 is passed to its local variable a.
As the local variable of putValue() method and instance variable have the same name, a. Hence, a=a puts 10 in the
local variable a and the instance variable a is not accessed at all.
Note : This issue is also known as shadowing as a local variable in a method has overshadowed an instance variable, by having a same name.
Using this keyword to resolve the shadowing issue.
To use this keyword to access a currently executing object and its instance variables, to resolve the shadowing issue.
// Example of this keyword in Java
class A
{
int a;
public void putValue(int a)
{
//Accessing an instance variable with this keyword, to differ it from a local variable with same name
this.a=a;
}
public static void main(String... ar)
{
A ob = new A();
ob.putValue(10);
System.out.println("Value in a : "+ ob.a);
}
}
Output-
Value in a : 10
In the putValue() method, we have accessed an instance variable named a with this keyword,
which has distinguished its name from a local variable named a to resolve the shadowing issue.