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

The execution result is inconsistent with the document description

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Centos、jdk-21.0.1、idea-IC-233.14808.21

      A DESCRIPTION OF THE PROBLEM :

      【Document Description】

      https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html

      Local Classes Are Similar To Inner Classes
      Local classes are similar to inner classes because they cannot define or declare any static members. Local classes in static methods, such as the class PhoneNumber, which is defined in the static method validatePhoneNumber, can only refer to static members of the enclosing class. For example, if you do not define the member variable regularExpression as static, then the Java compiler generates an error similar to "non-static variable regularExpression cannot be referenced from a static context."

      Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

      You cannot declare an interface inside a block; interfaces are inherently static. For example, the following code excerpt does not compile because the interface HelloThere is defined inside the body of the method greetInEnglish:

          public void greetInEnglish() {
              interface HelloThere {
                 public void greet();
              }
              class EnglishHelloThere implements HelloThere {
                  public void greet() {
                      System.out.println("Hello " + name);
                  }
              }
              HelloThere myGreeting = new EnglishHelloThere();
              myGreeting.greet();
          }
      You cannot declare static initializers or member interfaces in a local class. The following code excerpt does not compile because the method EnglishGoodbye.sayGoodbye is declared static. The compiler generates an error similar to "modifier 'static' is only allowed in constant variable declaration" when it encounters this method definition:

          public void sayGoodbyeInEnglish() {
              class EnglishGoodbye {
                  public static void sayGoodbye() {
                      System.out.println("Bye bye");
                  }
              }
              EnglishGoodbye.sayGoodbye();
          }

      【Java Code】

      package org.jdk21.example;

      import org.junit.jupiter.api.Test;

      import static java.util.FormatProcessor.FMT;

      public class LocalClassExample {

          @Test
          public void test() {
              defineLocalClass();
          }
          public void defineLocalClass() {

              interface LocalInterface {
                  void print();
              }
              class LocalInterfaceImpl implements LocalInterface {
                  @Override
                  public void print() {
                      System.out.println(FMT."""
                      \{LocalInterfaceImpl.class.getName()},
                      \{LocalInterfaceImpl.class.getCanonicalName()}
                              """);
                  }
                  public static void printClass() {
                      System.out.println(LocalInterfaceImpl.class.getTypeName());
                  }
              }
              new LocalInterfaceImpl().print();
              LocalInterfaceImpl.printClass();
          }
      }


      /*
       * org.jdk21.example.LocalClassExample$1LocalInterfaceImpl,
       * null
       *
       * org.jdk21.example.LocalClassExample$1LocalInterfaceImpl
       *
       * Process finished with exit code 0
      */



      REGRESSION : Last worked in version 21


      FREQUENCY : always


            asajeev Anjana Sajeev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: