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?

5
  • 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) Commented Oct 30, 2013 at 22:58
  • There is no single answer to this. For multiple answers, do a google search, see docs.oracle.com/javase/tutorial/java/javaOO/nested.html, stackoverflow.com/a/2148749/18573,http://stackoverflow.com/a/… have some good info Commented Oct 30, 2013 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. Commented Oct 30, 2013 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. Commented Oct 30, 2013 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) Commented Oct 31, 2013 at 8:00

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. Or even in the same grade, or school.
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).

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.