< Prev
Next >

Java this Keyword

Have you ever thought about how to access the currently executing object of a class in Java? In case you didn't know about it, don't worry because we are going to discuss just that and a lot more relevant in this article.

Using this keyword, we can access the currently executing object and its instance variables.

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

public static void main(String... ar)
A ob = new A();
System.out.println("Value in a : "+ ob.a);


Value in a : 10

Here in the putValue() method, we have accessed an instance variable a with this keyword, in order to distinguish its name from a local variable a, which resolves the shadowing issue.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< Static Keyword
Encapsulation >

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page


Please check our latest addition