Java class constants, after modifying the value of the static constant, does not take effect

Modify a constant class static constant value , and then upload the constants class class file to the server does not take effect, restart the service several times, the last query relevant information, using a GUI tool decompiler decompile view the compiled source code, finally found problem lies in:

Simply modify the static constant is useless, but also to recompile all references to the class static variable (or re-upload these categories server), or will not take effect.

Here involves the Java Virtual Machine compiler mechanism : the Java file, point to static compile-time constants static final, it will be parsed as a partial constant value at runtime (that is static constant after compilation, became a constant, instead of the original code). This (e.g., int, float, etc.) and java.lang.String applicable to all the fundamental data types.

Consider the following example of a:

Various constants used in the program with a class Contants.java to unified management.

public class Contants{
    public static final String STR = "csyor.com";
    //......
}

In certain logic code segment is then used in these constants. This will facilitate the management constants, avoid a lot of the magic number / text, just change it a class when you modify the contents of the constant on the line.

import Contant;
public class SomeService {
    public void doSome() {
        // 这里应用了上一个类的静态常量Contants.STR
        System.out.println(Contants.STR);
    }
}

In the above code, SomeService Contants.STR class uses this constant, the value of the surface is achieved by Contants class, actually after SomeService compiled, the code has changed, as follows:

import Contant;
public class SomeService {
    public void doSome() {
        // 这里把静态常量Contant.STR直接编译成为了一个常量"csyor.com"
        System.out.println("csyor.com");
    }
}

We found that there is simply nothing Contants class, so even if we modify the constant Contants class, let STR = "www.csyor.com", but SomeService still in print "csyor.com", because of SomeService Code without any modification, in the case of incremental compilation, its class file is not recompiled.

Here it may be asked: Why did you debug it easy to use in the local?

Mainly because local general use IDE (Integrated Development Environment) is automatically compiled, and we upload to the server when generally only upload the class file after the java compiler files you have changed (that is, contain class static constants), as for the other class reference this static constant because there is no altered so it will not upload, there have been no problems for local debugging, but there is no effect of the situation on the server.

See above these, the problem has been resolved, attention: no matter what the class is to replace the file, then we need to restart the middleware container.

Solution one: simply modify the static variable STR is useless, but also to recompile all quoted static variables STR classes (and then re-upload to the server), or will not take effect , that is to use the constant class to recompile.

Solution two: remove final

When using the static constant must be careful, because once modify it, you need to place the quoted static constant throughout the project to recompile replaced.

Published 23 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42046751/article/details/98188355