0

If a package-private top-level class (or interface) is used by only one class, consider making the top-level class a private nested class of the sole class that uses it (Item 22)

Effective Java 2nd edition

Referring to the above item from effective Java. I understand that it will promote class inaccessible for other purposes. But I have two doubts on this,

  1. Doing this, we will be moving a class which does a different purpose into other class. For example, an UI class which needs an event listener which is used only by that class, has to be moved into that UI class as per the above recommendation. Then the UI class will be less cohesive by principle as it serves UI rendering and event listening.

  2. Referring to the same example above, if we move the classes used only once as an inner class, then we are closed for extension and open for modification, say we want to change a different listener or inject dynamic listeners then we may not be achieving without modifying the UI class. Hence how feasible is the above approach to do?

Community
  • 1
  • 1
Rengasami Ramanujam
  • 1,858
  • 4
  • 19
  • 29
  • 6
    The keyword here is **consider**. As you saw yourself, there are several cases where it probably shouldn't be done. – Kayaman Jul 03 '15 at 06:56
  • Note the recommendation didn't use the word "inner class". "nested class" != "inner class". You talk about inner classes in your point 2. – Erwin Bolwidt Jul 03 '15 at 07:11
  • @JarrodRoberson Voted to re-open (it may need to be closed as "Asking for an opinion" though). Just because I make a comment about confusion between nested an inner classes, doesn't mean that that OP's question is the same as that! – Erwin Bolwidt Jul 03 '15 at 07:14

1 Answers1

3

When I read that book, I took that paragraph this way:

If a class is used by another class and there is no other way it would be used by some other class by any means, move it to the class that uses it as an inner class. This usage renders the inner class as a "private" feature of encapsulating class. Therefore, in order to keep things tidy, somewhat similar to encapsulation, the writer advised to make the move.

From that point of view, it made sense to me.

In short, encapsulate where ever it makes sense but don't force it if it does not.

Alp
  • 3,027
  • 1
  • 13
  • 28
  • thanks for the response. But my question is, doing it that way we are preventing future extension rt? The inner class may not be reused today but how about future and also the scenarios I mentioned in the question. – Rengasami Ramanujam Jul 03 '15 at 08:18
  • Agile thinking is against developing software for possible requirements of future. It rather advocates to base your design on todays requirements. And if there need be, you can change it: i.e. move your class outside and make it a stand along first class citizen and open it to reuse. – Alp Jul 03 '15 at 10:09
  • But then the basic thumb rule of design principle "Class should be open for extension and closed for modification" itself is against Agile thinking. Its not that I am arguing against Joshua Bloch. I just want to ensure that people doesn't quote him in wrong examples. – Rengasami Ramanujam Jul 05 '15 at 06:52