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

Enum switch incorrectly initializes all enum switches in a class

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Using an switch-on-enum syntax causes all enums that are referenced by switch-on-enum to load. This is not allowed by JLS, as JLS in https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.4 says that "A class or interface will not be initialized under any other circumstance".

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the executable test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should throw null pointer exception without showing any text.
      ACTUAL -
      It shows `Bar` followed by `Foo`.

      ---------- BEGIN SOURCE ----------
      enum Foo {
          ;
          static {
              System.out.println("Foo");
          }
      }
      enum Bar {
          ;
          static {
              System.out.println("Bar");
          }
      }

      public class Example {
          public static void main(String[] args) {
              switch ((Foo) null) {}
              switch ((Bar) null) {}
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: