简体   繁体   中英

Why does this java code not give an “is already defined ” error?

Why does this java code not give an already defined error for y? I can understand why, because it is in a loop. But it does not seem logical.

class AlreadyDefined 
{
    public static void main(String[] args)
    {
        //int y = 0; //-- enable this for already defined error 
        for(int i = 0; i < 5; i++) 
        {
            int y = i; //not already defined 
            System.out.println(i);
        }
    }
}

If I run this code it results in an error:

class AlreadyDefined 
{
    public static void main(String[] args)
    {
        int y = 0;
        for(int i = 0; i < 5; i++) 
        {
            int y = i; //already defined 
            System.out.println(i);
        }
    }
}

Because you are declaring y twice. Once outside the loop int y = 0; and once inside the loop int y = i; .

You should replace int y = i; with y=i; . This should solve your problem.

First declared y variable covers a broader scope including your for loop block. So it is visible inside for loop already. Because of this, redefining it inside for loop throws compilation error

class AlreadyDefined 
{
public static void main(String[] args)
{
    int y = 0; //this variable is global in aspect to your for loop
    for(int i = 0; i < 5; i++) 
    {
        int y = i; //your y variable is visible in this for loop block
                   //so redefining it would throw a compiler error
        System.out.println(i);
    }
}
}

So if you really want to use variable y , then don't define it again, and remove the int type before it.

Short answer: You cannot define the same variable twice in the same scope. The scope of your for loop occurs inside the method's scope, so y is already defined.

Long answer: This is a misunderstanding of variable scope. I'll try to explain:

class AlreadyDefined // begin class scope
{

    public static void main(String[] args){ // begin method scope

        for(int i = 0; i < 5; i++){ // begin loop scope. 
            /// This scope also includes variables from method scope
            int y = i; 
            System.out.println(i);
        } // end loop scope

    } // end method scope

} // end class scope

I labeled the respective scopes in the above code and where they begin and end.

In the above example, once the for loop ends, int y goes out of scope and can no longer be referenced. So after end loop scope , System.out.println(y) will fail, as y no longer exists.

In the second example, y already exists in method scope. You cannot redefine a variable with the same name in a "child" scope, which is why your second example fails.

There is one exception to this, however. You may define a variable with an identical name to one defined in class scope. So this would work:

class AlreadyDefined // begin class scope
{
    static int y = 0; // Defining variable with identical name.

    public static void main(String[] args){ // begin method scope

        for(int i = 0; i < 5; i++){ // begin loop scope
            int y = i; // This still works
            System.out.println(i);
        } // end loop scope

    } // end method scope

} // end class scope

For class scope, if there is an identically named variable in method scope, you must qualify it with this . So to reference the class scoped y variable inside main , you would use this.y . This is why you will commonly see variables assigned in constructors and setters like this:

public class SomeClass {
    int property

    public SomeClass(int property) {
        this.property = property;
    }

    public void setProperty(int property) {
        this.property = property;
    }
}

Another example:

    public void setProperty(int property) {
       property = property;
    }

If you were to do this, this would have no effect, as you did not specify this.property . You'd simply be setting the value of property to itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM