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(); obj1.method(); } } |
Output:
Value of Instance variable :10 Value of Local variable :40
This article is contributed by Twinkle Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Donât stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Instance variable as final in Java
- Static methods vs Instance methods in Java
- Instance Initialization Block (IIB) in Java
- Variable Arguments (Varargs) in Java
- Swap two Strings without using third user defined variable in Java
- Using predefined class name as Class or Variable name in Java
- Using _ (underscore) as variable name in Java
- Final static variable in Java
- Local Variable Type Inference or LVTI in Java 10
- Unreachable statement using final and non-final variable in Java
- Initialization of local variable in a conditional block in Java
- What is the difference between field, variable, attribute, and property in Java
- How to get the value of System Property and Environment Variable in Java?
- Why non-static variable cannot be referenced from a static method in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
Improved By : dangeti ganesh 1

