Variable Hiding in Java

Variable hiding happens when there is another variable with the same name and it has nearest scope.

Note :
When local and global variables have the same name, local variables will take precedence than global variables.


Consider the below program

  1. package com.kb.javainsimpleway.variable.hiding;
  2.  
  3. public class VariableHiding {
  4.     int x=100;
  5.     public static void main(String[] args) {
  6.         VariableHiding obj = new VariableHiding();
  7.         obj.displayX(200);
  8.     }
  9.     private void displayX(int x) {
  10.         System.out.println(x);
  11.     }
  12. }
package com.kb.javainsimpleway.variable.hiding;

public class VariableHiding {
	int x=100;
	public static void main(String[] args) {
		VariableHiding obj = new VariableHiding();
		obj.displayX(200);
	}
	private void displayX(int x) {
		System.out.println(x);
	}
}


Yes Output is 200 not 100.

In this program, we have used variable name as “x” for both local and instance variable.

In this case, the method displayX() is considering “x” as local variable and hiding the instance variable.

This is called variable hiding in the same class and hence value 200 will be printed not 100.

How to overcome from variable hiding?

We can overcome by using this keyword.

When we use same variables for local and instance variable, this will not be added by compiler, we need to add it explicitly to specify the instance variables.

So using this in the above program inside displayX() as below

  1. package com.kb.javainsimpleway.variable.hiding;
  2.  
  3. public class VariableHiding {
  4.     int x=100;
  5.     public static void main(String[] args) {
  6.         VariableHiding obj = new VariableHiding();
  7.         obj.displayX(200);
  8.     }
  9.     private void displayX(int x) {
  10.         System.out.println(this.x);
  11.     }
  12. }
package com.kb.javainsimpleway.variable.hiding;

public class VariableHiding {
	int x=100;
	public static void main(String[] args) {
		VariableHiding obj = new VariableHiding();
		obj.displayX(200);
	}
	private void displayX(int x) {
		System.out.println(this.x);
	}
}


Variable Hiding in child class

  1. package com.kb.javainsimpleway.variable.hiding;
  2. class Parent{
  3.     int x=10;
  4.     static int y=20;
  5. }
  6. class Child extends Parent{
  7.     int x=100;
  8.     static int y=200;
  9. }
  10. public class VariableHidingInheritance {
  11.     public static void main(String[] args) {
  12.         Child child = new Child();
  13.         System.out.println(child.x);
  14.         System.out.println(child.y);
  15.     }
  16. }
package com.kb.javainsimpleway.variable.hiding;
class Parent{
	int x=10;
	static int y=20;
}
class Child extends Parent{
	int x=100;
	static int y=200;
}
public class VariableHidingInheritance {
	public static void main(String[] args) {
		Child child = new Child();
		System.out.println(child.x);
		System.out.println(child.y);
	}
}

We have variables with the same name in both parent and child class, Hence parent variables gets inherited to the child class but hidden in the child class

Consider the below program

  1. package com.kb.javainsimpleway.variable.hiding;
  2. class Parent{
  3.     int x=10;
  4.     static int y=20;
  5. }
  6. class Child extends Parent{
  7. }
  8. public class VariableHidingInheritance {
  9.     public static void main(String[] args) {
  10.         Child child = new Child();
  11.         System.out.println(child.x);
  12.         System.out.println(child.y);
  13.     }
  14. }
package com.kb.javainsimpleway.variable.hiding;
class Parent{
	int x=10;
	static int y=20;
}
class Child extends Parent{
}
public class VariableHidingInheritance {
	public static void main(String[] args) {
		Child child = new Child();
		System.out.println(child.x);
		System.out.println(child.y);
	}
}

We can see that variables in the parent class gets inherited to the child class but they will be hidden as we declared the variables with the same name in the child class.

Note:
Variable hiding also happens when variables have the same name and different data type.

Consider the below Example

  1. package com.kb.javainsimpleway.variable.hiding;
  2. class Parent{
  3.     int x=10;
  4.     static int y=20;
  5. }
  6. class Child extends Parent{
  7.     float x=100.0f;
  8.     static int y=200;
  9. }
  10. public class VariableHidingInheritance {
  11.     public static void main(String[] args) {
  12.         Child child = new Child();
  13.         System.out.println(child.x);
  14.         System.out.println(child.y);
  15.     }
  16. }
package com.kb.javainsimpleway.variable.hiding;
class Parent{
	int x=10;
	static int y=20;
}
class Child extends Parent{
	float x=100.0f;
	static int y=200;
}
public class VariableHidingInheritance {
	public static void main(String[] args) {
		Child child = new Child();
		System.out.println(child.x);
		System.out.println(child.y);
	}
}


Note

Variable hiding happens when there is another variable with the same name

By default Java compiler adds this to the variable name (only when there is no local variable with the same name)

When the instance variable is hidden by the local variable, use this explicitly to access the instance variable

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on