Public fields in public classes do not respect the encapsulation principle and have three main disadvantages:
To prevent unauthorized modifications, private attributes and accessor methods (set and get) should be used.
Public fields can be modified by any part of the code and this can lead to unexpected changes and hard-to-trace bugs.
Public fields don’t hide the implementation details. As a consequence, it is no longer possible to change how the data is stored internally without impacting the client code of the class.
The code is harder to maintain.
This rule ignores public final fields because they are not modifiable. Also, annotated fields, whatever the annotation(s) will be
ignored, as annotations are often used by injection frameworks, which in exchange require having public fields.
Depending on your need there are multiple options:
get and set followed by the name of the field. Note: as a bonus it is now possible to monitor value changes
using breakpoints. public final if it is not supposed to change.
public class MyClass {
public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked
public String firstName; // Noncompliant
}
public class MyClass {
public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
By having a setter and a getter the code can control how the field is accessed and modified. For example, adding validation in the setter method will ensure that only valid values are set.
The access modifiers on the setter can also be changed to private or protected to restrain which code can modify the
value.