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

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

Consider this piece of code:

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


        
3条回答
  •  无人及你
    2021-02-20 09:10

    You've missed the word 'contain'. PrivateInnerClass is a member of your anonymous class and it is contained within it, so it cannot itself have an access modifier, under rule 14.3.

    It can have access modifiers for its own members, but you haven't explored that.

    The Eclipse error message wrongly describes it as local.

    You've also missed the point that 'private' would add nothing even if it were legal, as the inner class is invisible anyway.

提交回复
热议问题