What Are Shadow Variables in Java

What are Shadow Variables in Java?

Instead of providing my own description i may ask you to read about it for example here: http://en.wikipedia.org/wiki/Variable_shadowing. Once you understood the shadowing of variables i recommend you proceed reading about overlaying/ shadowed methods and visibility in general to get a full understanding of such terms.

Actually since the question was asked in Terms of Java here is a mini-example:

    public class Shadow {

private int myIntVar = 0;

public void shadowTheVar(){

// since it has the same name as above object instance field, it shadows above
// field inside this method
int myIntVar = 5;

// If we simply refer to 'myIntVar' the one of this method is found
// (shadowing a seond one with the same name)
System.out.println(myIntVar);

// If we want to refer to the shadowed myIntVar from this class we need to
// refer to it like this:
System.out.println(this.myIntVar);
}

public static void main(String[] args){
new Shadow().shadowTheVar();
}
}

What is variable shadowing used for in a Java class?

The basic purpose of shadowing is to decouple the local code from the surrounding class. If it wasn't available, then consider the following case.

A Class Foo in an API is released. In your code you subclass it, and in your subclass use a variable called bar. Then Foo releases an update and adds a protected variable called Bar to its class.

Now your class won't run because of a conflict you could not anticipate.

However, don't do this on purpose. Only let this happen when you really don't care about what is happening outside the scope.

Variable shadowing in Java

Variables aren't polymorphic. When you access m.name, that will always use the Mammal.name field which is part of that object, regardless of the execution-time type of the object. If you need to get access to Zebra.name, you need an expression with a compile-time type of Zebra.

The makeNoise method is called virtually though - the implementation used at execution time does depend on the type of the object.

Note that if you make all your fields private - which is generally a good idea anyway - this doesn't end up being an issue.

This is actually hiding rather than shadowing. See JLS section 8.3 for details on hiding, and section 6.4.1 for shadowing. I can't say that I always keep the differences straight...

Shadowing instance variables with local variables in Java

I thought that "x = x" in the constructor would be like "23 = 23".

Within the constructor, the meaning of the simple name x is always just the parameter. So the assignment x = x in the constructor takes the value of the x parameter and assigning it to the x parameter as well. The instance variable is never touched. (It's not clear what you mean by 23 = 23;, so I can't tell whether or not that's accurate.) Basically, this is a no-op and some IDEs will give you a warning about it.

To force it to copy to the instance variable, you want:

this.x = x;

(And likewise for y, of course.)

Variables shadowing in Kotlin for inner classes: how variables are resolved?

When you do var a = 2 inside the class A, you're not actually shadowing any variable. You're declaring that a is a field of class A, and it defaults to 2.

When you reference a variable in front of a class, Kotlin will add an implicit this (e.g. a becomes this.a) if there is a field with that name but not an upper-level variable with that name. So, the outer a takes precedence over the field, and you'd have to use this.a to access the inner a.

Java method overriding and variable shadowing

Method calls are virtual in Java, which means that the method from an object's actual type is called, regardless of what type the reference was which you used to access the object. Direct field access on the other hand is not virtual, so which r you access will depend on the type of the reference through which you reached it.

Variable and Method shadowing in Java

In your sub class (BedroomFlat), compiler will not allow you to declare a instance method with the same name as that of static method in the base class because method overriding is only applicable to instance methods. Extending a class only make instance methods available to the sub class for overriding(and not class methods i.e. static).
Moreover, when you try to declare a method with same signature as that of a static method, compiler will throw an error saying you cannot override a static method, as overriding takes place for instance method.

But compiler will not stop you from declaring a instance variable with the same name as static one from super class, because variables are not the candidates for overriding.



Related Topics



Leave a reply



Submit