Recently I was discussing in my project about package-private visibility modifier.
"Wait, what is package-private in Java?" It's also sometimes called default or no modifier.
This is when class or method doesn't have any modifier. For example:
class SomeObject {
void doSomething() {
...
}
}
There are more details in Java tutorial.
It should be not used unless very-very specific reason. You either choose between public, private or protected.
There are reasons I've heard so far from different people:
- But I need it to test method in my object!
- But It's better to restrict access to the Object as much as possible! It's being accessed only by same-package classes.
- I have library with lots of users and want to hide implementation details from them!
But I need it to test method in my object!
Don't do package-private just to test that very important method. It's one of the frequent fallacies which I observe. People tend to get somehow access to that private logic, just relax it a little bit and then do dirty testing!
There is a better way. First you need to understand that something probably wrong with your design, if there is a some hidden functionality which is hard to get to test. It means that it's a perfect candidate for refactoring! Just extract that logic into inner class with well-defined API methods and test them.
But It's better to restrict access to the Object as much as possible! It's being accessed only by same-package classes.
Official Oracle documentation says:
Use the most restrictive access level that makes sense for a particular member.
Use private unless you have a good reason not to.
I am totally agree with this statement, just if we will drop package-private out of this list. Life will be much easier for your project if everything will be public/private. One of the most frequent refactorings in large codebases is Move File/Class
. With lots of package-private access it will become immediately annoying to do so. Why not to make them public then?
At the end both public and package-private expose API of your class. No matter that in latter case it's limited by package, it's still an API which you have to care about.
When it still makes sense
- Tests. It's relatively new trend to not to write access modifiers for test code. Anyways modifiers bring some noise to the code and access visibility doesn't matter so much in tests as it does in production code. Less typing is better!
- You are supporting library with lots of users. Unless your library should be compatible with java8 or less! Otherwise I encourage to use JPMS (Project Jigsaw) in your libs, which will give you much better control over your exposed API's and internal implementation compared to package-private
So as you can see in 2019 (soon 2020!) there are not so much reasons to go with package-private visibility.
I don't think I'd agree with title statement - of course it's important to choose apropriate scope to specific case we're dealing with, but package scope is definetely being used too rarely.
Devs, left with choice either public or private tend to choose public to make things easier. But making class or method public it means it is part of your contract - functionality your package expose to other parts of code. If another part is using this class/method it's much harder to change/refactor it and code becomes tightly coupled.
An alternative is to make only one class in package public - and this class, facade, becomes your contract. All other classes can be package private. Then you test your package behaviour via facade class and refactoring is much easier.
Following this pattern makes also reading code easier - if you need only some functionality from package you only need to read facade class - and it's immediately visible - because it's public. Only if you need to change sth inside you need to go deeper and read another classes.
Architecture pattern that adapts this package-private style easily is hexagonal architecture, or ports-adapters architecture. You expose functionality of domain via public port, and rest of it is hidden with package-private scope. Following BDD you test domain via ports - like a black box.
Good talk about hex architecture and package scope :)
youtube.com/watch?v=sOaS83Ir8Ck