Advertisement



< 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
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 a, with this keyword, in order to distinguish its name from a local variable a, which resolves the shadowing issue.



Please share this article -





< Prev
Next >
< Static Keyword
Encapsulation >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement