What is a static nested class? [duplicate]

本秂侑毒 提交于 2019-12-04 02:29:34

问题


Possible Duplicate:
Java: Static vs non static inner class

What is a static nested class? What is the difference between static and non-static nested classes?


回答1:


A static inner class is a class nested inside another class that has the static modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of.

class Outer {
    private static int x;
    static class Inner1 {
    }
    class Inner2 {
    }
}

Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer -- you can't create an Inner2 without an Outer. You can create Inner1 object independently, though.

Code in Outer, Inner1 and Inner2 can all access x; no other code will be allowed to.




回答2:


See Java inner class and static nested class



来源:https://stackoverflow.com/questions/5805821/what-is-a-static-nested-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!