Best practices for using java system properties

Our code uses many system properties, such as' Java io. tmpdir’,’user. home’,’user. 'name', etc We don't define any constants for these anywhere (and I don't think) any other smart things to deal with them, so they are pure text scattered throughout the code

String tempFolderPath = System.getProperty("java.io.tmpdir");

How do you use system properties?

Solution

Like any other string constant like this, I will treat it as scattered throughout the code and define a constant variable In this case, "Java. Io. TMPDIR" is unlikely to change, but you will never know (I'm not saying that sun may change the meaning of "Java. Io. TMPDIR" or what system properties it points to, but you may change your mind to understand the system properties you need to read.)

If you use specific attributes in only one class, I will define constants in that class

private final String TEMPDIR = "java.io.tmpdir";

If you use the same properties in different classes, you may need to define your own static class to hold the most commonly used constants

public final Class Prop {
    public static final String TEMPDIR = "java.io.tmpdir";
    ...
}

Then, everywhere, you need to use the constant, just use it

System.getProperty(Prop.TEMPDIR);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>