Which part of JLS said anonymous classes cannot have public/protected/private member classes

前端 未结 3 2198
一向
一向 2021-02-20 08:29

Consider this piece of code:

public class TopLevelClass {
    Cloneable c = new Cloneable() {
        private int privateField;
        private void privateMethod         


        
3条回答
  •  梦毁少年i
    2021-02-20 08:46

    My final answer consists of two thesis:

    1. There is not strong declaration of restrictions for anonymous class members modifiers in the JLS. I.e. there isn't such part of JLS.

    2. But according to JVM specs anonymous classes aren't members of class:

    JVM 7 spec: 4.7.6 The InnerClasses Attribute states:

    If C is not a member of a class or an interface (that is, if C is a top-level class or interface (JLS §7.6) or a local class (JLS §14.3) or an anonymous class (JLS §15.9.5))...

    so, according to

    8.5 Member Type Declarations

    A member class is a class whose declaration is directly enclosed in another class or interface declaration.

    anonymous classes are not member classes.

    so, according to 8.1.1. Class Modifiers:

    The access modifiers protected and private (§6.6) pertain only to member classes within a directly enclosing class

    this classes aren't member classes, so they can not have mentioned modifiers.

提交回复
热议问题