Difference between static nested class and regular class

前端 未结 5 1134
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 07:52

I know this is a bit of a duplicate question but I want to ask it in a very specific way in order to clarify a very important point. The primary question being: Is there any

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 08:38

    A subtle difference is that a nested class has the ability to shadow members of the enclosing type:

    class ContainingClass {
        public static String privateStaticField = "a";
    
        static class ContainedStaticClass {
            public static String privateStaticField = "b";
            public static void main(String[] args) {
                System.out.println(privateStaticField);  // prints b
            }
        }
    }
    

    But that's also related to the scope of the class.

提交回复
热议问题