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 = 10
    void method()
        // This local variable hides instance variable
        int value = 40;
        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();


Value of Instance variable :10
Value of Local variable :40

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

This article is attributed to GeeksforGeeks.org


Java Java

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter