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

Access to final blank field before its initiation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u31
    • tools
    • x86
    • windows_xp

      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 ----------

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: