CPAMBF - Constant Private Attributes Must Be Final
Private attributes that never get their values changed
must be declared final
. By explicitly declaring them in such a way,
a reader of the source code get some information of how the
attribute is supposed to be used.
Wrong
int attr1 = 10;
int attr2 = 20;
void func () {
attr1 = attr2;
System.out.println(attr1);
}
}
Tip: Make final
all private attributes that never change.
Right
int attr1 = 10;
final int attr2 = 20;
void func () {
attr1 = attr2;
System.out.println(attr1);
}
}