As Rostislav Matl said, this is useful when you want to do something that is not part of your package interface.
As an example, suppose you have a package and it provides an interface and at least one specific implementation of a service.
People who use this service will take care of the interface you provide and use one of the specific classes that you provide, but they will not care about much other than that. Our service should talk to the database, and it should be able to display the result from database queries in its own data type (which form its contract).
I find that I regularly create classes in the package’s closed helper that contain methods like utilities or perform tasks such as the mapping we need. The default visibility (batch) is great for this because the other classes inside your package can use these helpers, but none of them can see them, so you can change them whenever you want.
This is an example of using some code:
We have our interface:
public interface UsefulService { Collection<DataThings> getThings(Identifier id); }
... and our specific implementation:
public class JdbcUsefulServiceImpl implements UsefulService {
Then we have our cartographer. We don’t need anyone outside the package to take care of the work of the service inside, so we use the individual visibility of the package and we can have as many classes as we want to execute:
class Mapper { Collection<DataThings> mapFromDatabaseToServiceDomain(DatabaseQueryResult queryResult){
The advantage that we have is that we can always change this Mapper class, but we either want to delete it, or create new private package classes, and we know that the only (immediate) effects that we can cause are inside this package. By direct effects, I mean compiler errors and such serious things. Obviously, you can break your service if you change its behavior, but what your automatic test suite should catch: P
Tom saleeba
source share