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

Eager enum class initialization with enum switch

    Details

      Description

      (See https://mail.openjdk.java.net/pipermail/compiler-dev/2019-February/012958.html )

      Steps to reproduce: compile and run the following program:

      // Test.java
      class Test {
        enum A{
          X;
          static { System.out.println("A is initialized!"); }
        }
        enum B{
          Y;
          static { System.out.println("B is initialized!"); }
        }

        static void testA(A a) { switch(a) {} }

        static void testB(B b) { switch(b) {} }

        public static void main(String[] args) {
          testA(A.X);
        }
      }

      Expected result:

      A is initialized!

      Actual result:

      A is initialized!
      B is initialized!

      According to JLS 12.4.1 the B enum type should not be initialized, as its fields and methods are not referenced during the program's execution. It appears that the mere declaration of method `testB(B)` is enough to make javac think, wrongly, that B needs to be initialized.

        Attachments

          Activity

            People

            • Assignee:
              jlahoda Jan Lahoda
              Reporter:
              tvaleev Tagir Valeev
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: