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

new javac doesn't initilize final instance variable

XMLWordPrintable

    • beta
    • generic
    • generic
    • Verified



      Name: dkC59003 Date: 07/13/99



      The section "8.3.2 Initialization of Fields" of JLS says:

      "If a field declarator contains a variable initializer, then it has the semantics
      of an assignment to the declared variable, and:
      ...

      -If the declarator is for an instance variable (that is, a field that is not static),
      then the variable initializer is evaluated and the assignment performed each time an
      instance of the class is created."

      Following test shows that JDK-1.3(F,G,H,I,K) javac does not generate proper code for
      initialization of final instance variable:

      ------------------- test.java-------------------
      public class test {
      public final String fnl_str = "Test string";

      public static void main(String args[]) {
      try{
      Class checksClass = Class.forName("test");
      Object checksObject = checksClass.newInstance();
      String reflected_fnl_str = (String)checksClass.getField("fnl_str").get(checksObject);
      System.out.println("fnl_str = " + reflected_fnl_str);
      }catch(Throwable e){
      System.out.println(e);
      }
      }
      }

      ------------------- runing -------------------
      %javac test.java
      %java test
      fnl_str = null
      (must be "fnl_str = Test string")

      ------------- disassembled test.class --------
      public class test
      {

      public final Field fnl_str:"Ljava/lang/String;" = String "Test string";

      public Method "<init>":"()V"
      stack 1 locals 1
      {
      aload_0;
      invokespecial Method java/lang/Object."<init>":"()V";
      return;

      // There is no assignment to final instance variable fnl_str
      // in the instance initializer

      }

      ..... method main() skiped .....

      } // end Class test

      --------------------------
      Correct code of the instance initializer could be like this

      public Method "<init>":"()V"
      stack 2 locals 1
      {
      aload_0;
      invokespecial Method java/lang/Object."<init>":"()V";
      aload_0;
      ldc String "Test string";
      putfield Field fnl_str:"Ljava/lang/String;";
      return;
      }


      -------------------------------------------------------

      The following JCK1.2.2 tests are failed due to this bug:

      vm/jni/GetSetField/gsfl001/gsfl00101m2/gsfl00101m2.html
      vm/jni/GetSetField/gsfl001/gsfl00101m3/gsfl00101m3.html
      vm/jni/GetSetField/gsfl001/gsfl00101m4/gsfl00101m4.html
      vm/jni/GetSetField/gsfl001/gsfl00101m5/gsfl00101m5.html
      vm/jni/GetSetField/gsfl001/gsfl00101m6/gsfl00101m6.html
      vm/jni/GetSetField/gsfl001/gsfl00101m7/gsfl00101m7.html
      vm/jni/GetSetField/gsfl001/gsfl00101m8/gsfl00101m8.html
      vm/jni/ToReflectedField/torf001/torf00101m1/torf00101m1.html
      vm/jni/ToReflectedField/torf001/torf00101m2/torf00101m2.html
      vm/jni/ToReflectedField/torf001/torf00101m3/torf00101m3.html
      vm/jni/ToReflectedField/torf001/torf00101m4/torf00101m4.html
      vm/jni/ToReflectedField/torf001/torf00101m5/torf00101m5.html
      vm/jni/ToReflectedField/torf001/torf00101m6/torf00101m6.html
      vm/jni/ToReflectedField/torf001/torf00101m7/torf00101m7.html
      vm/jni/ToReflectedField/torf001/torf00101m8/torf00101m8.html
      vm/jni/FromReflectedField/frrf001/frrf00101m1/frrf00101m1.html
      vm/jni/FromReflectedField/frrf001/frrf00101m2/frrf00101m2.html
      vm/jni/FromReflectedField/frrf001/frrf00101m3/frrf00101m3.html
      vm/jni/FromReflectedField/frrf001/frrf00101m4/frrf00101m4.html
      vm/jni/FromReflectedField/frrf001/frrf00101m5/frrf00101m5.html
      vm/jni/FromReflectedField/frrf001/frrf00101m6/frrf00101m6.html
      vm/jni/FromReflectedField/frrf001/frrf00101m7/frrf00101m7.html
      vm/jni/FromReflectedField/frrf001/frrf00101m8/frrf00101m8.html
      vm/jni/GetFieldID/gfid001/gfid00101/gfid00101.html

      ======================================================================

      Name: ssC76496 Date: 09/01/99

      new javac initializes final instance variable in build
      (1.3.0-D).
      ###@###.###,Sep 1 ,1999
      ======================================================================

            wmaddoxsunw William Maddox (Inactive)
            dkhukhrosunw Dmitry Khukhro (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: