Could someone please explain why Java rule squid:
S2039 (Member variable visibility should be specified) exists in the first place? Perhaps I'm missing something but this seems like a terrible rule that would encourage developers to violate object-oriented design best practices. The rule fires on any field or method which lacks an explicit
access control modifier (
private /
protected /
public). By default members are visible only within the same package (i.e. package private), and in many cases that is the optimal design. If a developer makes such a member
private then that would force her to write duplicate code rather than reusing that member across multiple classes in the package. Or if a developer makes such a member
protected or
public then it becomes part of the exposed API, thus violating the principles of encapsulation and information hiding.
One could argue that the lack of an explicit "package private" access control modifier represents a design flaw in the Java Language Specification. However even if that is the case it wouldn't be appropriate for static analysis tools to have a rule against something which is explicitly allowed in the JLS.