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

scoping rules are either broken or documented incorrectly

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.0beta
    • CPU:
      sparc
    • OS:
      solaris_2.4
    • Verification:
      Not verified

      Description


      class guy {
          void foo(boolean GandS, boolean playing_blackjack) {
        if (GandS) float HMS_Pinafore;
        while (playing_blackjack) double down;

        down = HMS_Pinafore;
          }
      }

      -- errors --
      guy.java:7: Undefined variable: down
                down = HMS_Pinafore;
                ^
      guy.java:7: Undefined variable: HMS_Pinafore
                down = HMS_Pinafore;
                       ^
      2 errors
      Exit 1

      From: Guy Steele

      There does remain a problem, however. The Java specification of March 15,
      1995, says (sectino 8.1):

      Declarations can appear anywhere that a statement is allowed.
      The scope of a declaration ends at the end of the enclosing block.

      I conclude that the following is a valid Java program:

      {
      if (GandS) float HMS_Pinafore;
      while (playing_blackjack) double down;
      ...
      down = HMS_Pinafore;
      }

      because I have put the declarations where statements may appear
      (as the body of an "if" or "while" statement) and the scope of
      the variables "HMS_Pinafore" and "down" ends at the end of the
      enclosing block, delimited by the braces in the example. Is my
      conclusion correct, or is the specification in error?

      --Guy

        Attachments

          Activity

            People

            Assignee:
            fyellinsunw Frank Yellin (Inactive)
            Reporter:
            ahoffsunw Arthur Hoff (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: