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

constant static fields cannot be referenced from const, instance initializers

    XMLWordPrintable

Details

    • b54
    • generic
    • generic

    Description

      In Enum spec reference to non-constant static field from its constructors, instance initializer blocks or instance variable initializer expressions is disallowed.

      But with the current implementation even reference to constatnt static field from constructor, instance initializer blocks or instance variable initializer expression is disallowed.
      It should be allowed or Spec needs to be changed.

      Tried with build Tiger-beta2-build51

      Please see the code below:- It should compile, but it is being flagged as compile time error.

      enum Students {

         GRAD,
         MCA {
               int h_local = h;
               String str_local = str;
               
               public int getInt() {
                   return h_local;
               }
               
               public String getString() {
                   return str_local;
               }
         };
         
         Students() {
             System.out.println("Const The value of k is :"+k);
             System.out.println("Const The value of str is :"+str);
         }
         
         final static int k =89;
         final static String str = "Happy Working";
         
         int h = k;
         String str_dup = str;
         
           {
             System.out.println("Instance Initializer block the value of k is :"+k);
             System.out.println("Instance Intializer block the value of str is :"+str);
           }
           
          public int getInt() {
            return h;
          }
          
          public String getString() {
              return str_dup;
          }
           
         
      }



      public class GeneralTest7 {

           boolean flag = true;
           
           public void assertion1() {
               Students enum_ref = Enum.valueOf(Students.class,"GRAD");
               if((enum_ref.getInt() == 89) & (enum_ref.getString().equals("Happy Working"))) {
                   System.out.println("Test sub assertion1 Passed");
               } else {
                   System.out.println("Test sub assertion1 Failed");
                   flag = false;
               }
               enum_ref = Enum.valueOf(Students.class, "MCA");
               if((enum_ref.getInt() == 89) & (enum_ref.getString().equals("Happy Working"))) {
                   System.out.println("Test sub assertion2 Passed");
               } else {
                   System.out.println("Test sub assertion2 Failed");
                   flag = false;
               }
               if(flag) {
                  System.exit(0);
               } else {
                  System.exit(1);
               }
               
           }
           
           public static void main(String args[]) {
               GeneralTest7 ref = new GeneralTest7();
               ref.assertion1();
           }

      }

       

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: