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

jvm spec does not define certainly which methods cause the class initialization

    Details

    • Subcomponent:
      vm
    • Resolved In Build:
      1.2fcs
    • CPU:
      sparc
    • OS:
      solaris_2.5.1
    • Verification:
      Not verified

      Description



      Name: szC45993 Date: 06/02/98



      The revised jvm spec says:
       
      "2.16.4 Initialization

      Initialization of a class consists of executing its static initializers (?2.11) and the initializers for
      static fields (?2.9.2) declared in the class. Initialization of an interface consists of executing the
      initializers for fields declared in the interface (?2.13.4).

      Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by
      the class need not be initialized. Similarly, the superinterfaces of an interface need not be
      initialized before the interface is initialized.

      A class or interface type T will be initialized immediately before one of the following occurs:

         T is a class and an instance of T is created.
         T is a class and a static method of T is invoked.
         A non-constant static field of T is used or assigned. A constant field is one that is (explicitly
         or implicitly) both final and static, and that is initialized with the value of a
         compile-time constant expression. A reference to such a field must be resolved at compile
         time to a copy of the compile-time constant value, so uses of such a field never cause
         initialization.

      Invocation of certain reflective methods in class java.lang.Class and in package
      java.lang.reflect also causes class or interface initialization."

      The list of these reflective methods should be defined certainly.
      In another way, it contradicts to the main principle of java which sounds
      as "compile once and use everywhere".

      The test below shows that jdk1.2b4H does not provide the class
      initialization at the java.lang.reflect package method Array.newInstance()
      use but, for example, the jdk1.2b3A jvm provides class initialization
      in this case:


      novo7% setenv CLASSPATH /export/home1/JAVA/tmp/../classes:/export/ld22/java/oul/JT20/javatest.jar:/export/home1/JAVA/workdir4/classes:/export/ld22/java/gvv/JT20/classes
      novo7% /export/ld14/java/dest/jdk1.2b4H/solaris/bin/java -verify javasoft.sqe.tests.vm.concepts.execution080.execution08001.execution08001
      execution08001b.iii =333
      execution08001b.iii =333
      novo7% echo $status
      97
      novo7% /export/ld14/java/dest/jdk1.2b3A/solaris/bin/java -verify javasoft.sqe.tests.vm.concepts.execution080.execution08001.execution08001
      execution08001b.iii =333
      execution08001b.iii =777
      novo7% echo $status
      95
      novo7%



      SOURCE:


      --------------------- execution08001.java
      //File: @(#)execution08001.java 1.3 98/04/14
      //Copyright 04/14/98 Sun Microsystems, Inc. All Rights Reserved

      package javasoft.sqe.tests.vm.concepts.execution080.execution08001;

      import java.io.PrintStream;
      import java.lang.reflect.Array;

      class execution08001b {

         static int iii = 333;

      }

      class execution08001a {

         static {
      execution08001b.iii = 777;
        }

      }

      public class execution08001 {

         public static int run(String argv[], PrintStream out) {
      String cs = null;
              try {
      int dim[] = {3, 5, 7};

      out.println("execution08001b.iii ="+execution08001b.iii);
      if (execution08001b.iii != 333) return 2;

      Object ob = java.lang.reflect.Array.newInstance( execution08001a[][][][][][][].class, dim );

      out.println("execution08001b.iii ="+execution08001b.iii);
      if (execution08001b.iii != 777) return 2;

              } catch (Throwable e) {
      out.println(e);
                  return 2;
              }

              return 0;
        }

        public static void main(String argv[]) {
        System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }

      }
      ======================================================================

      Hook 5(hook5): test

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

        Attachments

          Activity

            People

            • Assignee:
              gbrachasunw Gilad Bracha (Inactive)
              Reporter:
              zsssunw Zss Zss (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: