Instance Variable Hiding in Java

Course Curriculum

Instance Variable Hiding in Java

Instance Variable Hiding in Java

In Java, if there is a local variable in a method with same name as instance variable, then the local variable hides the instance variable. If we want to reflect the change made over to the instance variable, this can be achieved with the help of this reference.

class Test
{
// Instance variable or member variable
private int value = 98;

void method()
{
// This local variable hides instance variable
int value = 65;

System.out.println("Value of Instance variable :"
+ this.value);
System.out.println("Value of Local variable :"
+ value);
}
}

class UseTest
{
public static void main(String args[])
{
Test obj1 = new Test();
obj1.method();
}
}
Output:

Value of Instance variable :98
Value of Local variable :65

Overriding toString() in Java (Prev Lesson)
(Next Lesson) Static blocks in Java
', { 'anonymize_ip': true });