• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

What is Variable Hiding and Variable Shadowing in Java

August 31, 2023 //  by Talha Malik

In Java programming: local, instance, and static are three distinct kinds of variables. Local variables are declared inside a method/function and are only visible within that scope while the instance variables are declared outside of methods and are accessible to all functions in the class. Static variables are declared/created with the static keyword and are directly accessible by all objects of the class. This article will discuss in detail what is variable shadowing and hiding and how it works.

Variable Hiding and Shadowing in Java

Variable shadowing and variable hiding occur when two variables with the same/identical name are created in different scopes. Shadowing occurs when an inner variable hides an outer variable. Hiding occurs when an instance variable hides a static variable.

Let us discuss the code for a clear understanding.

Example 1: Variable Hiding in Java

In this code, variables with the same names are created to explain the concept of variable hiding in detail.

class vehicle //parent class
{
    String mystr = "Car";
  }//child class wheels extending parent class vehicle
class wheels extends vehicle
{
    String mystr = "Truck";
}
class variablehiding
{
public static void main(String args[])
{
    wheels objects = new wheels();
    System.out.println("mystr is: "+objects.mystr);
   
}
}

In the above code:

  • Two classes named “wheels” and “vehicles” are declared.
  • Both classes have declared the variable with the same name.
  • The output will have the inherited value.

Output

The following output has inherited the variable of its parent’s class therefore “truck” is printed as the output:

Example 2: Variable Shadowing in Java 

This code will illustrate how the shadowing occurs when an inner variable hides an outer variable:

class variableshadowing {
int c = 15;
class instance {
int c=10;
public void print()
{
System.out.println(c);
System.out.println(variableshadowing.this.c);
}
}
}
class shadowing {
public static void main(String[] args)
{
variableshadowing object = new variableshadowing();
variableshadowing.instance instanceObject= object.new instance();
instanceObject.print();
}
}

In the above code:

  • There are two classes declared.
  • The instance class is declared within the “variableshadowing” class.
  • The two classes contain integers declared with the same variable name.

Output

The following output depicts that the variable that was declared within the instance class is printed first, and then the output for the main class that is variable shadowing is printed:

This sums up the information related to variable hiding and shadowing.

How to Manage Variable Hiding and Variable Shadowing in Java?

To manage variable hiding and shadowing, a simple way is to avoid using the same variable for declaration. If the variables are the same the compiler gets confused as to which variable has been called. Therefore in variable hiding the compiler takes the value of local variable first instead of global. When the instance variable and the global variables are the same, the term refers to variable shadowing.  In order to avoid variable shadowing different variables have to be used for simplicity.

Conclusion

In Java, the term Variable hiding refers to a situation where the child class and parent class use the same variable even if the types are different. Variable shadowing happens in Java when local variables and the instance variables are the same. This blog post has demonstrated what variable hiding and shadowing are and how to manage them in Java.

Category: Java

Previous Post: « How to Use ZonedDateTime now() Method in Java
Next Post: How to Implement remove() Method of ThreadPoolExeceutor in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact