Why you should avoid package-private scope in Java
Tags: java, oop
Published: 2019-11-26 13:39:55

---
title: Why you should avoid package-private scope in Java
published: true
description: 
tags: java, oop
cover_image: https://thepracticaldev.s3.amazonaws.com/i/8dklq82i1qwpa7ghicu3.jpg
---



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] (https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html).

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.