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

VM does not check if a final field is initialized twice

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.2.1
    • Fix Version/s: None
    • Component/s: hotspot
    • Labels:
    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.4

      Description



      Name: akC45999 Date: 04/01/99



      The chapter 2.9.1 Field Modifiers of the revised JVMS reads:

      A field can be declared final, in which case its declarator must include a
      variable initializer (?2.9.2). Both class and instance variables (static and
      non-static fields) may be declared final. Once a final field has been
      initialized, it always contains the same value.

      The test below shows that VM does not check neither if a final field
      is initialized nor if it always contains the same value.

      ------------------------------ test.java ----------------------------

      import java.io.PrintStream;

      public class test {

        public static void main(String a[]) {

      BadClass.someMethod();
      System.out.println("Bad");
        }
      }

      ---------------------------- BadClass.jasm --------------------------

      public class BadClass
      {

      static final Field finalField:I;

      public static Method someMethod:"()V"
      stack 1 locals 0
      {
      iconst_0;
      putstatic Field finalField:"I";
      return;
      }
      }

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

      novo48% java -fullversion
      java full version "JDK-1.2-V"
      novo48% java test
      Bad

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

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

      Name: akC45999 Date: 04/01/99



      There is more suitable test case:

      ------------------------------ test.java ----------------------------

      import java.io.PrintStream;

      public class test {

        public static void main(String a[]) {

      BadClass.someMethod(1);
      System.out.println("finalField = " + BadClass.finalField);
      BadClass.someMethod(2);
      System.out.println("finalField = " + BadClass.finalField);
        }
      }

      ---------------------------- BadClass.jasm --------------------------

      public class BadClass
      {

      static final Field finalField:I;

      public static Method someMethod:"(I)V"
      stack 1 locals 1
      {
      iload_0;
      putstatic Field finalField:"I";
      return;
      }
      }

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

      novo48% java test
      finalField = 1
      finalField = 2

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              coleenp Coleen Phillimore
              Reporter:
              duke J. Duke (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: