in Java EE ~ read.

is package-private the new private?

Just a few days ago I attended the DEVOXX conference in Belgium. The last session I attended was from Adam Bien’s session Real World Java EE.

As usual i was amazed by the speed and manner of presentation performed by Adam. Just 5 slides and the rest is on the keyboard and a IDE. Juggling between questions from the audience and the message he wants to be passed. Besides that I was triggered (and more people I think) by the statement that he does not use the private keyword anymore. Whats that, I always learned that you should hide the internals of your class. Is the open closed principle not violated with this practice?

What does package private mean:
From the specification;

If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.

If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package.

So when no modifier is specified it is not accessible outside the package, thus rendering it effectively private.
The better argument from Adam is that by this way creating test cases becomes much simpler. There is no need for Reflection or anything else to mock your class under test.

What about modularity, Kirk Knoernschild presented Architecture All the Way Down. From this presentation modularity (reducing and getting insights in effects of a change). Is a key to creating maintainable systems. With modules in mind there are clear boundaries between parts of the code.

Is the package private the new private?

I do think so. why?

  • From Kirk we get clear boundaries in packages/modules thus the package private is no issue.
  • Test cases are simpler, No reflection needed.
  • Closed for modification; Assuming that you do not place the extension in the same package the members are still private
  • Open for modification

Question remaining, how long will it be before the development community picks this up?