I could not find any references online about this. But just wanted to know if final fields in a class should always be static
or is it just a convention. Based
They don't always come together and it's not a convention. final
fields are often used to create immutable types:
class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
On the other hand static
but not final
fields are not that common and are quite tricky. static final
is seen often because it means application1-wide constant.
1 - well, class loader-wide, to be precise