Modularity in Java: top level vs. nested classes

99封情书 提交于 2019-12-23 09:50:48

问题


The Java tutorials that I read, like to use nested classes to demonstrate a concept, a feature or use.

This led me to initially implement a sample project I created just like that: Lots of nested classes in the main activity class.

It works, but now I got a monstrous monolithic .java file. I find it somewhat inconvenient and I now intend to break to multiple .java files/classes.

It occurred to me, however, that sometimes there may be reasons not to take classes out of their enclosing class.

If so, what are good reasons to keep a module large, considering modularity and ease of maintenance?

Are there cases in which it is impractical (or even impossible) to convert a nested class to a toplevel class? In other words, is there a case in which only a nested class could satisfy certain functionality?


回答1:


a non-static nested class has an implicit reference to the creator instance of the enclosing class, and also it can access every member of the enclosing class (even private members). You lose this if you make the nested class top-level:

public class Outer {
    private String s;

    public void setS(String s) {
        this.s = s;
    }

    public class Inner {
        public String getOuterS() {
            // This is legal only if Inner is
            // non-static and nested in Outer
            return s;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer o = new Outer();
        o.setS("Hello world!!!");

        // i now has access to every o member
        Outer.Inner i = o.new Inner();

        // Prints "Hello world!!!"
        System.out.println(i.getOuterS());
    }
}



回答2:


It can be easier to read all the classes if they are in the same file. This is why this approach is good for example code.

However for real code, you should break your files/classes into manageable sizes. The longest class file in Java 6 is about 9000 lines long. I tend to keep classes shorter than this. ;)




回答3:


Yes. Inner classes (non-static nested classes) may refer to instance variables of the containing class.

Also, nested classes may access private members of the containing class.




回答4:


In addition to the benefit of closure (already pointed out in an other answer), nested classes also help you achieve multiple implementation inheritance (ref: Thinking in Java, page 369 - section "Why inner classes"?). As far I know, there is no other way to achieve it. So, if your nested class is helping you achieve multiple implementation inheritance, then it would not be possible for you to make the nested a top-level class.

Nested classes allow you to cleanly separate some functionality that belongs to the outer class and at the same time keep that functionality close to the outer class. In such cases, nested classes provide the best option from a design perspective and that alone can be the reason for not making it a top-level class (which can lead to class pollution in the main package).



来源:https://stackoverflow.com/questions/5354728/modularity-in-java-top-level-vs-nested-classes

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