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`.
|