I was wondering, if I have this field in my class : private final int foo = ...
, should I put it in static private static final int foo = ...
? Beca
There is a big difference between the two non-access modifiers.
final
means your variable can be assigned a value once. static
sets its scope to pertain to the class (rather than an instance or a local context). There is no reason why they should go together unless by design.
A static final
variable is de facto a constant.
For instance, fields declared in interface
s are implicitly public
, static
and final
.
Amongst the usage examples for final
, non static
fields you can find: