top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+2 votes
687 views
What is a compile time constant in Java? What is the risk of using it?
posted Apr 1, 2016 by Deepak Jangid

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Compile time constant and Risk

Compile time constant

public static final variables are known as compile time constant, 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

Problem with this is that if you are using 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 new version of JARs.

To avoid that, make sure you compile your program when you upgrade dependency JAR files.

answer Apr 1, 2016 by Karthick.c
Similar Questions
+2 votes

I assume that constant must be initialized to a constant value at compile time, but following code the return value of ge_const() is assigned to x which will be collected at run time. So this must cause an error but I am getting the output as 50. Can someone clarify the detail?

main()
{
    const int x = get_const();
    printf("%d", x);
}
int get_const()
{
    return 50;
}
0 votes

Given a sentence and a word, remove all occurrences of the word in the sentence.

For example, removing “is” from the sentence “This is a boy.” becomes “Th a boy.”

...