5

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
an00b
  • 11,338
  • 13
  • 64
  • 101

4 Answers4

4

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. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Lawery, also breaking the code in manageable files increase performance of the system. – palAlaa Mar 18 '11 at 16:38
  • @Alaa, Making the classes more managable can give you more time to improve things like performance. Simpler shorter code often runs faster, but I don't think there is a direct performance impact of using larger classes. – Peter Lawrey Mar 18 '11 at 16:46
  • Lawery, I knew that it's affected with files indexing . – palAlaa Mar 18 '11 at 16:50
2

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());
    }
}
gpeche
  • 21,974
  • 5
  • 38
  • 51
1

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).

Dheeru Mundluru
  • 373
  • 3
  • 10
1

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151