In Java, shadowing occurs when a variable declared in a certain scope (e.g., a method or a block) has the same name as a variable declared in an outer scope (e.g., a class field or a method parameter). The inner variable “shadows” or “hides” the outer variable within that scope, meaning that the inner variable will be used instead of the outer one for the duration of that scope.
### Example of Shadowing in Java:
“`java
class ShadowingExample {
int x = 10; // Instance variable (field)
void method() {
int x = 20; // Local variable (shadows the instance variable)
System.out.println(“Local x: ” + x); // Will print 20
System.out.println(“Instance x: ” + this.x); // Will print 10 (refers to the instance variable)
}
public static void main(String[] args) {
ShadowingExample obj = new ShadowingExample();
obj.method();
}
}
“`
### Output:
“`
Local x: 20
Instance x: 10
“`
### Explanation:
– The instance variable `x` is declared in the class at the top.
– In the `method()` method, a local variable `x` is declared, which **shadows** the instance variable `x`.
– Inside `method()`, when we refer to `x`, it refers to the local variable `x` (value `20`), unless explicitly referencing the instance variable using `this.x`, which still holds the original value (`10`).
### Key Points about Shadowing:
1. **Variables**: Shadowing can occur with instance variables, class variables (static variables), and method parameters.
2. **Method Parameters**: If a method parameter has the same name as an instance variable, the parameter will shadow the instance variable within the method.
3. **Accessing Outer Variable**: If you want to access the outer variable that is shadowed, you can use `this` (for instance variables) or `super` (for superclass variables).
4. **Shadowing and Inheritance**: Shadowing can also happen in subclasses, where a subclass declares a variable with the same name as one in the superclass.
### Example of Shadowing with Method Parameters:
“`java
class Person {
String name = “Alice”; // Instance variable
void setName(String name) { // Parameter shadowing the instance variable
System.out.println(“Local name: ” + name); // Local parameter value
this.name = name; // Referencing the instance variable using ‘this’
}
void displayName() {
System.out.println(“Instance name: ” + this.name);
}
public static void main(String[] args) {
Person person = new Person();
person.setName(“Bob”); // Setting a new name using the parameter
person.displayName(); // Output will show “Bob”
}
}
“`
### Output:
“`
Local name: Bob
Instance name: Bob
“`
In this example:
– The method `setName` has a parameter `name` which **shadows** the instance variable `name`.
– `this.name` refers to the instance variable, and `name` (without `this`) refers to the method parameter.
### Conclusion:
Shadowing is a common behavior in Java, especially when dealing with variables that have the same name in different scopes. It is important to be mindful of it to avoid confusion and ensure you’re referring to the correct variable.