blob: 5aed60286bd06341491b9757bd4e1c9e09ff383d [file] [view] [edit]
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`.