Jayson
Jayson

Reputation: 2031

What is variable shadowing used for in a Java class?

I'm reading my Deitel, Java How to Program book and came across the term shadowing. If shadowing is allowed, what situation or what purpose is there for it in a Java class?

Example:

public class Foo {

    int x = 5;

    public void useField() {
        System.out.println(this.x);
    }
    public void useLocal() {
        int x = 10;
        System.out.println(x);
    }
}

Upvotes: 27

Views: 34710

Answers (5)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

The two common uses are constructors and set methods:

public Foo(int x) {
    this.x = x;
}

public void setX(int x) {
    this.x = x;
}

Very occassionally it's useful if you want a copy of the variable at a single instant, but the variable may change within the method call.

private void fire() {
    Listener[] listeners = this.listeners;
    int num = listeners.length;
    for (int ct=0; ct<num; ++ct) {
        listeners[ct].stateChanged();
    }
}

(Of course, a contrived example made unnecessary with the posh for loop.)

Upvotes: 3

Yishai
Yishai

Reputation: 91931

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.

Upvotes: 43

z  -
z -

Reputation: 7178

Shadowing is not really a java only term. In any instance where a variable declared in a scope has the same name as one in a bigger scope, that variable is shadowed.

Some common uses for shadowing is when you have inner and outer classes and want to maintain a variable with the same name.

If you can avoid it though, you should since it may cause confusion.

Upvotes: 6

Tom Jefferys
Tom Jefferys

Reputation: 13310

It can be useful for setters where you don't want to have to create a separate variable name just for the method parameter eg:

public void setX(int x) {
    this.x = x;
}

Apart from that I'd avoid them.

Upvotes: 17

Robert Munteanu
Robert Munteanu

Reputation: 68318

One major purpose is to confuse people. It's bad practice and should be avoided.

Upvotes: 10

Related Questions