Checker FieldAccess   as of Julia version 2.5.0 (built on 4 Jul 2018)

belongs to group Basic

Identify fields never read or never written


Fields constitute the state of an object, that is, they are meant to store information that survives across method calls. It is hence useless to store information in a field that is never read. Moreover, that behavior is actually harmful, since writing into a useless field hinders the garbage collector. It is also pointless to read data from a field that is never written, since the default value for the field will be found there. Note that there are situations when a field is written by reflection, which cannot be understood by Julia, in general. This is often the case of fields auto-wired through dependency injection. Julia knows some specific patterns, typically marked through code annotations, but cannot foresee all possibilities.

Action: remove all fields that are never read or never written.

Examples


Consider the following program:

public class Fields {
  private int f1;
  private final Object f2 = new Object();
  private float f3;

  public Fields() {
    System.out.println(f1);
  }

  public static void main(String args[]) {
    new Fields();
  }
}

This checker issues the following warnings:

Fields.java: [FieldAccess: FieldNeverReadWarning] Field Fields.f2 is never read in reachable code
Fields.java: [FieldAccess: FieldNeverUsedWarning] Field Fields.f3 is never used in reachable code
Fields.java: [FieldAccess: FieldNeverWrittenWarning] Field Fields.f1 is never written in reachable code

In this example, all three fields are useless and could be removed, reducing the memory footprint of the instances of the class and helping the garbage collector.