I have seen much code where people write
public static final String mystring = ...
final indicates that the value of the variable won't change - in other words, a variable who's value can't be modified after it is declared.
public final static String when you want to create a
static: no instance necessary to use it), and that
final), for instance when you want to define a
Stringconstant that will be available to all instances of the class, and to other objects using the class.
public final static String MY_CONSTANT = "SomeValue"; // ... in some other code, possibly in another object, use the constant: if (input.equals(MyClass.MY_CONSTANT)
public static final int ERROR_CODE = 127;
It isn't required to use
final, but it keeps a constant from being changed inadvertently during program execution, and serves as an indicator that the variable is a constant.
Even if the constant will only be used - read - in the current class and/or in only one place, it's good practice to declare all constants as
final: it's clearer, and during the lifetime of the code the constant may end up being used in more than one place.
final may allow the implementation to perform some optimization, e.g. by inlining an actual value where the constant is used.