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

4.12.4: Clarify effectively final for local variables

    XMLWordPrintable

Details

    Description

      JLS8 4.12.4 defines "effectively final" for a local variable in a rather roundabout fashion:
      --
      A local variable or a method, constructor, lambda, or exception parameter is effectively final if it is not declared final but it never occurs as the left hand operand of an assignment operator (§15.26) or as the operand of a prefix or postfix increment or decrement operator (§15.14, §15.15).

      In addition, a local variable whose declaration lacks an initializer is effectively final if all of the following are true:
      • It is not declared final.
      • Whenever it occurs as the left-hand operand of an assignment operator, it is definitely unassigned and not definitely assigned before the assignment; that is, it is definitely unassigned and not definitely assigned after the right-hand operand of the assignment (§16 (Definite Assignment)).
      • It never occurs as the operand of a prefix or postfix increment or decrement operator.
      --

      Here is a clearer version: (Note the switch from "declaration" to "declarator", and "assignment operator" to "assignment expression". '=' is an assignment operator; it is used in both assignment expressions and local variable declarators, but those are not the same thing.)
      --
      • A local variable whose declarator has an initializer (14.4.2) is effectively final if all of the following are true:
      - It is not declared final.
      - It never occurs as the left hand side in an assignment expression (§15.26). (Note that the local variable declarator containing the initializer is _not_ an assignment expression.)
      - It never occurs as the operand of a prefix or postfix increment or decrement operator (§15.14, §15.15).

      • A local variable whose declarator lacks an initializer is effectively final if all of the following are true:
      - It is not declared final.
      - Whenever it occurs as the left hand side in an assignment expression, it is definitely unassigned and not definitely assigned before the assignment; that is, it is definitely unassigned and not definitely assigned after the right hand side of the assignment expression (§16 (Definite Assignment)).
      - It never occurs as the operand of a prefix or postfix increment or decrement operator.

      • A method, constructor, lambda, or exception parameter is treated, for the purpose of determining whether it is effectively final, as a local variable whose declarator has an initializer.

      Attachments

        Activity

          People

            abuckley Alex Buckley
            abuckley Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: