This is the fourth day of my participation in the More text Challenge. For details, see more text Challenge

1, the background

In my daily work, after a few days will make a hair, to solve the production environment to meet all kinds of bugs and optimization, but because of some accumulate over a long period, some project update, replace the jar package can not be the whole package, usually is the replacement of the class a single package, try to let the old system will be quite a lot of time, But in the process of a release, met a little problem, we only modified the constant class, so only to update the a class at that time, modify the content found in the testing process is not effective, because constant practical after some trying to find the problems caused by improper, below is for this on pit experience, talk about in Java constants and measures from the pit.

2. What is a constant

A constant is a data whose value remains the same throughout the operation. It is usually given directly in a command or program. The data types used as a constant are numeric, character, date, logical, and currency.

Constants in Java

In the Java language, we use the final keyword to define constants. We usually decorate this with public static final.

    package com.kevin.test01;

    public class Const
    {
      public static final String sun = "SUN";
      public static final String supperFileType = "exe; jar; class";
    }
Copy the code

4. Example debugging

Let’s write an instance of a constant

package com.kevin.test01;

public class Const {
	 public static final String sun = "SUN";
         public static final String supperFileType = "exe; jar; class";
}

Copy the code
package com.kevin.test01;

public class Common {
	public static void main(String [] args){
		StringBuffer txt = new StringBuffer();
		txt.append("Supported File Types"); txt.append(Const.supperFileType); System.out.println(txt.toString()); }}Copy the code

The running results are as follows:

Let’s decompile these two classes using the decompile tool JD-GUI to see what the compiled content looks like

So let’s look at the contents of the Const class. There’s no problem here.

We look at the Common class reference constants, we found that after decompiling, where the reference constants is not the constant name, but a constant value, if we have a lot of places in the code references constants in this way, it can only be updated jar package for the whole package, but also increase the risk of other problems in, So how do you avoid accidentally digging or stepping on this kind of hole.

5. One of the suggestions about the use of constants

1. Do not refer to a constant name directly. Instead, add the get method to the constant class.

package com.kevin.test01;

public class Const {
	 static final String sun = "SUN";
	 static final String supperFileType = "exe; jar; class";
	 public static String getConst(String constName){
	    switch(constName){
	    	case "sun" :
                    return sun;
		case "supperFileType":
                    return supperFileType;
	    	default: 
                    return null; }}}Copy the code

Where we need to reference constants, we get them directly using static methods in the constant class

package com.kevin.test01;

public class Common {
	public static void main(String [] args){
		StringBuffer txt = new StringBuffer();
		txt.append("Supported File Types");
		txt.append(Const.getConst("supperFileType")); System.out.println(txt.toString()); }}Copy the code

Here’s what it looks like when decompiled,

After decompiling, we see that instead of referring to a constant value, we call a static method, which is a perfect way to avoid the uncertainty of direct reference. There are many other very good methods, which will be discussed in more detail later in this article.