I've seen some code in a project recently where some fields in a couple classes have been using the default access modifier without good reason to. It almost looks like a case of "oops, forgot to make these private". Since the classes are used almost exclusively outside of the package they are defined in, the fields are not visible from the calling code, and are treated as private. So the mistake/oversight would not be very noticeable.
However, encapsulation is broken. If I wanted to add a new class to the existing package, I could then mess with internal data in objects using fields with default access.
So, my questions:
In terms of style, I have seen some style guides which recommend putting "package" in a comment to denote that it was intentional. Like this:
/*package*/ int myInt;
It's unfortunate that the Java language spec doesn't allow use of "package" explicitly; after all, it's already a reserved keyword!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With