Recently, I saw some code in a project when some fields in class pairs used the default access modifier without any reason. It is almost like the case of "oops, forgot to make it private." Since classes are used almost exclusively outside the package in which they are defined, the fields are not visible from the calling code and are treated as private. Therefore, error / supervision will not be very noticeable.
However, encapsulation is impaired. If I wanted to add a new class to an existing package, I could then use the internal data in the objects using the default access fields.
So my questions are:
- Is there any best practice regarding default access specifiers that I should be aware of? Anything to help prevent a recurrence of this type of accident?
- Are there any annotations that can say something about the impact of “I really meant it to be default access”?
- Using CheckStyle or any other Eclipse plug-ins, is there a way to specify instances of default fields or to prohibit any "// default access" comments that are not accompanied by, say, terminating them?
source
share