I am studying java and wanted to know what's the usage of static final variables in application designing. Please provide some examples too.
Where are static final variables used in java?
570 views Asked by AudioBubble At
3
There are 3 answers
0
On
final means the reference can't be changed once it's set. static means the variable belongs to a class, not a specific instance. This combination of modifiers, especially when used on a primitive or an immutable class (such as String) are often used to represent constants. A classic example would be java.io.File.separtorChar.
Uses of a
static finalvariable :Integerclass defines a constant calledMIN_VALUEas :public static final int MIN_VALUE = 0x80000000;public static finalreference.When you mark a variable defined in a class as
static, it is shared across all the instances of aclass. When you mark a variable in a class asfinal, it can only be initialized once either on the same line as the declaration of the variable or in the constructor of the class. Putting both together, a memberstatic finalvariable is shared across all instances of aclass, must be initialized where it is declared, and cannot be modified once declared.