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

scoping rules are either broken or documented incorrectly

XMLWordPrintable

    • 1.0beta
    • sparc
    • solaris_2.4
    • Not verified


      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

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: