Variable Hiding in Java

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

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

Consider the below program

1
2
3
4
5
6
7
8
9
10
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);
    }
}
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);
	}
}

What is the output ?

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 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
2
3
4
5
6
7
8
9
10
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);
    }
}
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);
	}
}

We get the output as


Variable Hiding in child class

Look at the below program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
    }
}
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);
	}
}

Output of the above program is

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
2
3
4
5
6
7
8
9
10
11
12
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);
    }
}
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);
	}
}

Output is

We can see that variables in the parent class gets inherited to the child class but just that 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
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
    }
}
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);
	}
}

Output is


Key points to remember

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