Why are private fields on an enum type visible to the containing class?

前端 未结 3 1159
清酒与你
清酒与你 2021-02-20 12:44
public class Parent {

    public enum ChildType {

        FIRST_CHILD(\"I am the first.\"),
        SECOND_CHILD(\"I am the second.\");

        private String myChild         


        
3条回答
  •  野性不改
    2021-02-20 13:04

    A top level class is like a village, everybody knows everybody, there are no secrets. Nested units within it don't change that fact.

    class A
        private a;
    
        class B
            private b
    
            a = x; // ok
    
        new B().b = y; // ok
    
        class C extends A{}
    
        new C().a = z; // ok
    

提交回复
热议问题