Constant will not change after replacing class in Tomcat

I deployed the application to Tomcat 6, and after I deployed it, I wanted to make some changes to my constant class, and I only loaded the file with the constant class ( .class ) into the exploded war file.

And even after I restarted the server several times, the changes I made will not be displayed.

All I changed are strings in constants. What would you advise me to do except upload the war file again?

+4
source share
1 answer

You will have to recompile all classes that reference String constants.

Note that the static final field of a primitive or String type that is initialized with a compile-time constant value (the so-called constant variable ) will be embedded when they are used in other classes.

In other words, if you have these classes:

 public class Constants { public static final int FOO = 42; } public class Bar { public void frobnicate() { System.out.println(Constants.FOO); } } 

Then at compile time , the FOO value will be compiled into the .class Bar file, which means that Bar no longer refers to Constants at run time!

It also means that any change to FOO will not affect Bar until you recompile Bar with the new Constants.class .

This effect is discussed in detail in JLS ยง13.4.9 final Fields and Constants .

One way to avoid this problem in the future is to make sure that your "constants" are not interpreted by the compiler as constant variables. One way to do this is to move the value assignment from the initializer to a simple assignment through the static initializer block:

 public class Constants { public static final int FOO; static { FOO = 42; } } 
+7
source

Source: https://habr.com/ru/post/1347497/


All Articles