Why is this an issue?

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.

What is the potential impact?

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.

Exceptions

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.

How to fix it

Depending on your need there are multiple options:

Code examples

Noncompliant code example

public class MyClass {

  public static final int SOME_CONSTANT = 0;     // Compliant - constants are not checked

  public String firstName;                       // Noncompliant

}

Compliant solution

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;
  }

}

How does this work?

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.

Resources