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

Local class cannot be instantiated from a static context

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 24
    • tools
    • openjdk version "24-ea" 2025-03-18
      OpenJDK Runtime Environment (build 24-ea+30-3590)
      OpenJDK 64-Bit Server VM (build 24-ea+30-3590, mixed mode, sharing)

    • 24
    • x86
    • linux_ubuntu

      Consider:

      {code:java}
      public interface A {
        static A localSingleton() {
          class B implements A {
            private static final A INSTANCE = new B();
          }
          return B.INSTANCE;
        }
      }
      {code}

      The idea is to avoid exposing publicly class B (as classes declared in interfaces are `public` by default).

      The class above compiles with Java 23 and less, but fails with Java 24-ea+30 with:

      {noformat}
      A.java:4: error: local class B cannot be instantiated from a static context
            private static final A INSTANCE = new B();
                                              ^
      1 error
      {noformat}

            Unassigned Unassigned
            sbordet Simone Bordet
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: