[PRL] Java and shadowing local variable declarations

bjchamb at cc.gatech.edu bjchamb at cc.gatech.edu
Mon Jun 5 17:45:50 EDT 2006


Not sure if this will help, but I found the following online:

"You may not redefine a local variable inside a block nested within its
scope. Local variables may hide instance variables however. Any you may
reuse local variables inside blocks, so long as they don't nest."

Again, not written in the best English, but it seems to suggest that the
sentence is trying to say that you can't hide local variables with local
variables.

(Link to the website I got that off of
http://mindprod.com/jgloss/scope.html#GOTCHAS)

Given that it is illegal, it seems the sentence is almost backward. 
Trying to say that the local variable may not be redeclared inside of an
enclosed method.  Either that or they wrote the sentence starting at the
most nested level, and are saying that the actual redeclaration occurs at
the enclosing scope.  Whatever went wrong while they were writing that
sentence, it seems that variable hiding is just illegal.

-- Ben Chambers

> How does local-variable shadowing in Java 1.4 work?  (For the purposes
> of this conversation, a "local variable" is a variable that is declared
> inside a block, as distinct from a field in a class or interface, a
> method parameter, a constructor parameter, or an exception-handler
> parameter.)
>
> Because one can nest blocks arbitrarily deeply in Java, I had assumed
> that local variables declared in an inner block would shadow local
> variables declared in an outer block, and indeed the text in section
> 6.3.1 of the Java Language Specification, 2nd edition (JLS2) seems to
> support this.  (Keep in mind that method bodies and constructor bodies
> are considered blocks.)  However, the following class fails to compile:
>
>     public class Foo {
>         public static void main(String[] args) {
>             boolean x = true;
>             {
>                 int x = 42;
>                 System.out.println(x);
>             }
>         }
>     }
>
>     Foo.java:5: x is already defined in main(java.lang.String[])
>                 int x = 42;
>                     ^
>
> The relevant language appears to be in section 14.4.2 of JLS2:
>
>     The name of a local variable _v_ may not be redeclared as a local
>     variable of the directly enclosing method, constructor or
>     initializer block with the scope of _v_, or a compile-time error
>     occurs.
>
> (Italics theirs.)
>
> What does this sentence mean?
>
> Other than that the person who wrote that particularly tortured piece of
> English should be taken out behind the barn and shot, that is.
>
> Richard
>
> (Just to make this clear: I do in fact need to know the answer to this
> question; I'm not just venting about the bad writing in the JLS.)
>
> _______________________________________________
> PRL mailing list
> PRL at lists.ccs.neu.edu
> https://lists.ccs.neu.edu/bin/listinfo/prl
>




More information about the PRL mailing list