Why does my eclipse IDE run without compiler error when I create a non static inner class with static methods and variables?

Viewed 50

Member inner classes can’t have static members in them. When my friend ran the same code on her eclipse IDE she got a compiler error for a static member in a non static inner class. When I run the code there are no errors. Why?

This is the code:

public class Outer {
    class Inner {
        static void method() {
            System.out.println("Static method in Inner Class");
        }
    }
}
1 Answers

You probably have your compiler compliance level set to Java 16 (or above). The language specification was changed to allow static members and methods in inner classes in Java 16 - see JLS §8.1.3. This was included as part of the JEP 395

With the compliance level set to Java 15 or before Eclipse will reject static methods or members in a inner class.

Related