DEV Community

k@k
k@k

Posted on

What is a compile time constant in Java? What is the risk of using it?

Compile time constant and Risk

public static final variables are also known as a compile-time constant, the public is optional there. They are replaced with actual values at compile time because compiler know their value up-front and also knows that it cannot be changed during run-time.

Risk associate with Compile time constant

One of the problems with this is that if you happened to use a public static final variable from some in-house or third party
library and their value changed later than your client will still be using old value even after you deploy a new version of JARs. To avoid that, make sure you compile your program when you upgrade dependency JAR files.

Top comments (1)

Collapse
 
hophiducanh profile image
Ho Anh

Thank you. But I'm still wondering why Java doesn't allow runtime constant?