This question discusses the practices of non-static final variables.
My first impression is that non-static final variables are useless because final variables cannot be reassigned after initialization.
class Foo {
public static final int BAR = 10;
}
That would mean all the objects created from type Foo
would have BAR = 10
.
When would it be useful, practical, or accepted to use non static, but final variables, like so?
class Foo {
public final int BAR = 10;
}
So in what case does the latter become useful? Since it basically means the same thing.
When that same animal has a field like this:
private final Animal mother;
Everyone only has one biological mother and there is no shared mom, meaning it is specific for each instance.
Making it static
means that the value is shared amongst all instances. If this is what you want then it makes sense ofcourse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With