2

I have two Java classes located in different jars deployed on IBM Websphere App Server 7.0 (it is a bit old but I forced to use it on a project).

The first class has a static final field:

public class TFFVERSION {
    public static final String ACTUAL_VERSION = "8.3";
}

The second uses the field in a non-static method:

xslTemplate = String.format("%s_v%s", xslTemplate, TFFVERSION.ACTUAL_VERSION);

What I am trying is understand is why the compiler put the copy of constant 8.3 in String.format call? This makes impossible to just change the first class without recompiling and redeploying the second.

Is this a kind of optimization or a Bug in IBM Java V9?

Here I've opened class files in a text editor showing the problem (image is clickable for better resolution): enter image description here

Java decompilers show the correct static field invocation though. There are no other 8.3 constants in the second class.

ike3
  • 1,760
  • 1
  • 17
  • 26
  • Possible duplicate of https://stackoverflow.com/questions/5173372/java-static-final-values-replaced-in-code-when-compiling. – ike3 Jul 06 '18 at 13:40

1 Answers1

2

This is how it usually works, it is done by javac not by IBM's Java version. Create two classes where one uses a static final constant from the other and run, then recompile only the class with the constant and see what happens. Or look at the byte codes.

If you want to be able to change the constant down the line, expose it with a method and pay the small performance cost at runtime.

ewramner
  • 5,810
  • 2
  • 17
  • 33