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

Access to final blank field before its initiation

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 6u31
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_26"
      Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
      Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      This bug probably exists on every system.

      A DESCRIPTION OF THE PROBLEM :
      According to documentation (jsl3, page 527, first paragraph):
      "A Java compiler must carry out a specific conservative flow analysis to make
      sure that, for every access of a local variable or blank final field f, f is definitely
      assigned before the access; otherwise a compile-time error must occur."

      But this is not true. We can access field before it is initialized.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class Test {

      final String s;

      Test(String s) {
      System.out.println(getS()); //Access field before initiation
      this.s = s;
      }

      String getS() {
      return s;
      }

      public static void main(String[] args) {
      new Test("test");
      }
      }

      Compile this code and run it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation error.
      ACTUAL -
      Printed string: null

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Unfortunately none.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {

      final String s;

      Test(String s) {
      System.out.println(getS()); //Access field before initiation
      this.s = s;
      }

      String getS() {
      return s;
      }

      public static void main(String[] args) {
      new Test("test");
      }

      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: