Question

Say I have 20 classes in my root package. I decided to organize the classes by creating 3 subpackages under the root package. Then I put 5 classes into each of the subpackages, leaving 5 classes at the root package (because they are top level classes). Now because I need to use classes in subpackages from the 5 classes in the root package, I end up making classes in subpackages public, while before subpackaging those classes only have within package exposure.

The above code organization is done with good guidances. The subpackaging is by feature instead of by layer. The interdependency before packages are minimized. But still, a lot of subpackage classes make sense to be accessed from a level higher.

Is there a good practice addressing this type of scenarios?

Was it helpful?

Solution

Unfortunately not. The most you can have is package-level visibility by making it package-private (default access). You cannot have it so that a class is only visible to other classes within the same module. I think Project Jigsaw and JSR 294 will address this (in addition to other stuff), but you're probably not going to see it until Java 9.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top