I am writing my first "API jar" that will be open source library and used by (possibly) other developers. I've read Joshua Block's thesis on effective API design, and one of the things he talks about - that I never would have thought of otherwise - is his concepts of minimizing access and maximizing information hiding. Basically, you only want your API developers to have access to the Java objects they will be using, and you don't want your API developers to have access to any of the "guts" of your library.
In my several years as a Java developer, I've never had the need to make a class anything other than public
. Furthermore, I've never used nested classes either. So I'm sitting here wondering how do I implement this "information hiding" best practice in my Java API? And I think private, and possibly nested, classes is the answer. But where to begin?
- Every
.java
source file requires at least 1public
class in it to compile. So for me to make a classprivate
(and non-nested), I need to "bundle it with apublic
class. To me this makes sense only if thepublic
/private
classes are heavily related. But what if I have a section of my API that just consists ofprivate
classes (for accessibility-minimizing-purposes) that don't relate to any otherpublic
analogs? - When do you make a
private
class nested, and when do you make it non-nested? Or is it just a matter of preference?