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

One can refer static, const static variables from instance initializers of enum

XMLWordPrintable

    • b49
    • generic
    • generic

      As per the recent Enum spec (Preliminary Proposed Final Draft 2)
      Section III:- Semantics
      It is mentioned that:-

      It is illegal to reference a non-constant static field of enum type from its constructors, instance initializer blocks, or instance variable initializer expressions.

      With respect to Tiger-beta2 build 44 tested on Solaris-9 only:-

      The behavior is:-
      1) One can not refer any static or constant static filed of any type from constructor. Compile time error

      2) One can refer any static or constant static field of any type from instance initializer. No compile time error in this case, but fails at runtime.

      How to reproduce:-
      Execute the code mentioned below:-
      import java.util.*;

      enum Students {
        GRAD,
        PhD;
        
        Students() {
           System.out.println("Const is being called");
           System.out.println("Const the enum constant is :"+this.toString());
           
           //System.out.println("The value of k is :"+k);
           //System.out.println("The value of i is :"+i);
           // System.out.println("The value of h is :"+h);
           //studentsMap.put(toString(), this);
           
           
        }
        
         static final Map<String, Students> studentsMap = new HashMap<String, Students>();
         static int k = 100;
         final static int h = 500;
         final static Integer i =678;
         
         
          { // simple instance initializer block
            System.out.println("Initializer block is being called");
            
            System.out.println("The value of k is :"+k);
            System.out.println("The value of i is :"+i);
            System.out.println("The value of h is :"+h);
            System.out.println("Intializer block the enum constant is :"+this.toString());
            System.out.println("The value of studentsMap is :"+studentsMap);
            studentsMap.put(toString(), this);
            
          }
      }


      public class GeneralTest4 {
         
         public void assertion1() {
              Students enum_ref = Enum.valueOf(Students.class,"GRAD");
         }
         
         public static void main(String args[]) {
             GeneralTest4 ref = new GeneralTest4();
             ref.assertion1();
         }

      }

      Output got while executing:-
      Initializer block is being called
      The value of k is :0
      The value of i is :null
      The value of h is :500
      Intializer block the enum constant is :GRAD
      The value of studentsMap is :null
      Exception in thread "main" java.lang.ExceptionInInitializerError
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:495)
              at java.lang.Class.getEnumConstants(Class.java:2510)
              at java.lang.Class.enumConstantDirectory(Class.java:2531)
              at java.lang.Enum.valueOf(Enum.java:186)
              at GeneralTest4.assertion1(GeneralTest4.java:42)
              at GeneralTest4.main(GeneralTest4.java:47)
      Caused by: java.lang.NullPointerException
              at Students.<init>(GeneralTest4.java:33)
              at Students.<clinit>(GeneralTest4.java:4)
              ... 9 more

      Expected Output:- Should not compile

            gafter Neal Gafter (Inactive)
            jsinghsunw Jit Singh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: