0

I'm confused about when to make a Java class a:

  • Static nested class
  • Top-level class in the same package
  • Top-level class in another package

For example

class School {
    static class Grade {
        static class Class {
            static class Student {
            }
        }
    }
}

Is this a logically good design? It puts objects in logical layers. If classes aren't nested in this way, the logical layers pollute the namespace. A student may go elsewhere. Would creating a package for Student make it better?

Should this structure be nested or flattened?

vincentvanjoe
  • 767
  • 1
  • 12
  • 23
  • Generally if you use that specific class only in the outer class, you can keep it as nested class. Though, doesn't make sense why to create this hierarcy you have in the question. You can make all Grade,Class,Student as nested classes inside of School (if you want to stick to this design) – Ioan Oct 30 '13 at 22:58
  • There is no single answer to this. For multiple answers, do a google search, see http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html, http://stackoverflow.com/a/2148749/18573,http://stackoverflow.com/a/5355368/18573 have some good info – Miserable Variable Oct 30 '13 at 22:58
  • Flatten, use packages if you're worried about name spaces. If these classes start to have a lot of content, your design will be a file size horror. – Noctua Oct 30 '13 at 23:05
  • @Ioan Not necessarily / only, the other case is when the nested class needs access to private members, off the top of my head, the builder pattern for immutable classes. – TC1 Oct 30 '13 at 23:23
  • @TC1 well,I think you're wrong. If the inner class is static (nested), it will not have access to its outer private members. (only to static members) – Ioan Oct 31 '13 at 08:00

3 Answers3

2

In my opinion: Flatten. Because Students don't remain for ever in the same class.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

Flatten. This looks a little like you've have conflated the ideas of instance encapsulation with namespaces. A School instance may contain many references to Students, but there is no reason why the Student type should be scoped within the School type.

Student may mean student clothes else where.

Student should never mean anything other than student. It should not have nothing to do with clothes.

If we create a package for them, it'll be a little better.

Yes, packages are containers for organizing types. Types themselves should not be used in this way unless a nested type:

  1. Is so tightly coupled with the outer type that it would be meaningless on its own.

  2. Is a common concept manifested in a way that is specific to the outer class. For example, Map.Entry is appropriate as a nested, static type (interface) because 'entry' is such a general concept, and this particular type of entry is only relevant when dealing with maps.

  3. Is only used within its outer class and is not visible outside of it (or at least not visible outside of the package).

Mike Strobel
  • 25,075
  • 57
  • 69
0

Nested static classes are generally good when you want to "hide" their code from other classes, or when you want to associate that code with the enclosing class.

They are not commonly used for multi-level encapsulation as in your example, or when they are fairly large (say, over 100-200 lines), because they become difficult to maintain. In such cases, you should better use packages.

A nice overview of the 2 types of nested classes (static and non-static, aka inner) can be found in an earlier StackOverflow question, as well as in the Nested Classes section of the Java Tutorial.

Community
  • 1
  • 1
PNS
  • 19,295
  • 32
  • 96
  • 143