I am studying java and wanted to know what's the usage of static final
variables in application designing. Please provide some examples too.

- 297,002
- 52
- 306
- 350
-
1Well, constants, mostly... – Jon Skeet Jun 14 '15 at 06:35
-
Related: http://stackoverflow.com/questions/1415955/private-final-static-attribute-vs-private-final-attribute – Maroun Jun 14 '15 at 06:36
3 Answers
Uses of a static final
variable :
- To define a compile time constant value. e.g : The
Integer
class defines a constant calledMIN_VALUE
as :public static final int MIN_VALUE = 0x80000000;
- To create a non modifiable reference that can be accessed globally : E.g The Singleton pattern using a
public static final
reference.
When you mark a variable defined in a class as static
, it is shared across all the instances of a class
. When you mark a variable in a class as final
, 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 member static final
variable is shared across all instances of a class
, must be initialized where it is declared, and cannot be modified once declared.

- 15,069
- 6
- 45
- 82
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
.

- 297,002
- 52
- 306
- 350
You can define constants like
public static final String LANGUAGE = "JAVA"