Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8227010

Error recovery after local variable redeclaration can be improved

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 14
    • 14
    • tools
    • None
    • b05

      Consider code like this:
      ---
      public class T {
          {
               String v = "";
               Integer v = 0;
               System.err.println(v.byteValue());
          }
      }
      ---

      The errors for this code is:
      ---
      $ javac T.java
      T.java:4: error: variable v is already defined in instance initializer of class T
               Integer v = 0;
                       ^
      T.java:5: error: cannot find symbol
               System.err.println(v.byteValue());
                                   ^
        symbol: method byteValue()
        location: variable v of type String
      2 errors
      ---

      The first error is correct, of course, but the second error seems unnecessary - the variable is "redeclared", so it seems from the point of redeclaration, "v" should mean the new variable.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: