Why you should avoid package-private scope in Java

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() {
    ...
  }
}

Enter fullscreen mode Exit fullscreen mode

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.

原文链接:Why you should avoid package-private scope in Java

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容