< Prev
Next >



Java this Keyword











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

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 our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2