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

Different handling for final in constructor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 6
    • specification
    • rc
    • x86
    • linux
    • Verified

      FULL PRODUCT VERSION :
      java version "1.6.0_0"
      OpenJDK Runtime Environment (IcedTea6 1.6.2) (suse-0.1-x86_64)
      OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux xxx 2.6.25.20-0.7-default #1 SMP 2010-02-26 20:32:57 +0100 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      There is a different handling for final member in constructor: Accessing by this or without this lead to a compile error or does not.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      /tmp/java> javac FinalClass.java
      /tmp/java> java FinalClass foo
      before init : this.s == null
      after init : this.s == foo
      /tmp/java> javac FinalClass2.java
      FinalClass2.java:5: variable s might not have been initialized
      if (s != null) {
      ^
      1 error
      /tmp/java>

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I'd prefer to get a compile error for FinalClass, too. But according to http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5.2 neither should lead to a compile error.

      ACTUAL -
      FinalClass is compilable, FinalClass2 is not

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      FinalClass2.java:5: variable s might not have been initialized
      if (s != null) {
      ^
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class FinalClass {
      private final String s;

      public FinalClass(String ss) {
      if (this.s != null) {
      System.out.println("before init\t: this.s != null : " + this.s);
      } else {
      System.out.println("before init\t: this.s == null");
      }
      this.s = ss;
      System.out.println("after init\t: this.s == " + this.s);
      }

      public static void main(String[] args) {
      new FinalClass(args[0]);
      }
      }


      #######################################

      public class FinalClass2 {
      private final String s;

      public FinalClass2(String ss) {
      if (s != null) {
      System.out.println("before init\t: this.s != null : " + this.s);
      } else {
      System.out.println("before init\t: this.s == null");
      }
      this.s = ss;
      System.out.println("after init\t: this.s == " + this.s);
      }

      public static void main(String[] args) {
      new FinalClass2(args[0]);
      }
      }

      ---------- END SOURCE ----------

            abuckley Alex Buckley
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: