File: FieldCanBeLocal.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (38 lines) | stat: -rw-r--r-- 800 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
A field which is always assigned before being used from every method that reads
it may be better expressed as a local variable. Using a field in such cases
gives the impression of mutable state in the class where it isn't necessary.

```java
class Frobnicator {
  private int sum = 0;

  int sum(int a, int b) {
    sum = a + b;
    return sum;
  }

  int sum(int a, int b, int c) {
    sum = a + b + c;
    return sum;
  }
```

```java
class Frobnicator {
  int sum(int a, int b) {
    int sum = a + b;
    return sum;
  }

  int sum(int a, int b, int c) {
    int sum = a + b + c;
    return sum;
  }
```

## Suppression

This check is suppressed by `@SuppressWarnings("FieldCanBeLocal")`.

It will also be suppressed if the field is annotated with `@Keep` or an
annotation annotated with `@Keep`.