Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Shadowing

 
raj sekar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
Need some help on Variable Shadowing.
"Variables are shadowed not overrriden" :roll:
Thanks,
Raj
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajn,
Welcome to Javaranch
I'd like you to read the Javaranch Naming Policy and change your publicly displayed name. Thank you.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables can be shadowed or hidden but not overriden.
Please read JLS sections 6.3.1, 8.3 , 8.3.3.1 , 8.3.3.2 and 8.4.8.4
Basically in an field access expression the field accessed is always in the declared type of the reference in the expression:
Type variable = new SubType();
System.out.println(variable.field);
will print the field in the class Type. However:
System.out.println(variable.method());
will invoke the method defined in SubType if it happens to be a polymorphic one. That is, methods can be overriden because the JVM finds out the runtime type of the object pointed by variable when a polymorphic method is invoked on it, but not when a field is accessed on it.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, who can explain (clearly) the differences between hiding, shadowing, and obscuring. All three are referenced in the JLS. But the majority of the time, we talk about static fields and methods hiding the versions with the same signature in the superclass.
So what does it mean to "shadow" or "obscure" something, and how is this different than hiding?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 6.3.2 Obscured Declarations

Obscuring is distinct from shadowing (�6.3.1) and hiding (�8.3, �8.4.6.2, �8.5, �9.3, �9.5). The naming conventions of �6.8 help reduce obscuring.

A good way to figure this out is to go through the JLS index and search for Shadowing, Hiding and Obscuring. That way you get all possible references within the JLS where those terms occur and their exact meaning for each specific cases.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 6.3.2

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type or a package. In these situations, the rules of �6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

JLS 6.3.1

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.
...
Hiding, in the technical sense defined in this specification, applies only to members which would otherwise be inherited but are not because of a declaration in a subclass.
 
raj sekar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to share some more details on Variable shadowing.
Methods are overridden and Variables are shadowed.

The famous Car/SportsCar Question.
class Car
{
int gearRatio = 10;
String method() { return "From class Car "; }
}
class SportsCar extends Car
{
int gearRatio = 5;
String method(){ return "From class SportsCar"; }
public static void main(String[] args)
{
Car car = new SportsCar();
System.out.println("GearRatio = " + car.gearRatio + " " + car.method());
}
}

Why does it print 'GearRatio = 10 From class SportsCar' although I have overridden the variable gearRatio in SportsCar ???
The concept involved here is ' variables are shadowed and methods are overridden'.
This means that the variable to be selected depends on the declared class of the variable and method to be selected depends on the actual object's class the variable is refering to. In the above question, the declared class of variable 'car' is Car, so gearRatio of class Car is selected but as the class of actual object pointed to by variable 'car' is SportsCar, method() of SportCar is selected.
Consider this :
SportsCar car = new SportsCar();
System.out.println(car.gearRatio); //This will print 5, ie. from SportsCar.
System.out.println(car.method() ); //This will print 'From SportsCar, ie.again from SportsCar.
Now,
Car justCar = (Car) car;
System.out.println(justCar.gearRatio); //This will print 10, ie. from Car as the variable justCar is of class Car.
//So, here you are actually going behind the class SportsCar (By casting) and then accessing the gearRatio. So we say that the variable gearRatio of Car is shadowed by gearRatio of SportsCar.

System.out.println(justCar.method() ); //This will print 'From SportsCar, ie.still from SportsCar. You cannot go behind even by casting, so we say that method() of Car is overridden by method() of SportsCar.
Raj
 
Jeff Allen
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raj, for simplifying what was a difficult concept for me to grasp before.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, wait for the May newsletter which will include a nice article by Cindy Glass about shadowing, obscuring, and hiding...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic