Java Compile time constants

[Last Updated: Feb 12, 2016]


A variable of primitive type or type String, that is final and initialized with a compile-time constant expression, is called a constant variable.

public final int A_CONSTANT = 5;

All compile-time constants are inlined during compile time i.e. their references will be replaced with their values by the compiler both in the current class, and in other classes where they are used.

If we change the constant value in the defining class then we have to recompile all classes which are using it otherwise old value continue to be used.

Referring to a constant variable does not trigger class initialization.

JLS 4.12.4

See Also