Constants

Constants in Java


Constants in Java are used when a ‘static‘ value or a permanent value for a variable has to be implemented. Java doesn’t directly support constants. To make any variable a constant, we must use ‘static’ and ‘final’ modifiers in the following manner:


Syntax to assign a constant value in java:


static final datatype identifier_name = constant;

Example:

static final double pi=3.14;

The static modifier causes the variable to be available without an instance of it’s defining class being loaded

The final modifier makes the variable unchangeable

Java : Constants Program List