What is a static nested class? [duplicate]

♀尐吖头ヾ 提交于 2019-12-01 13:50:27

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.

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