What is the static constant in the class file?

Source: Internet
Author: User

Recently wrote a project encountered a problem, back and forth a few times, finally explored clearly. No nonsense, on the example.


Background: Because the project is small and the configuration file is not used, all static constants are placed inside the Config.java.

public class Config {public    static final String url= "http://www.xxxx.com/";    public static final int page_num=10;}


It looks good too, no big problem, it's all used.

Well, put it on the server and run it, normal.


Next, the question comes.

URL change, Page_num also changed.

public class Config {public    static final String url= "http://www.yyyy.com/";    public static final int page_num=200;}

OK, generate the next class file, put it on the server, have a cup of tea ...

Refresh under, unchanged, and so on, the server has a conversion time ...

30s ... Hasn't changed.

1min ... Hasn't changed.

Restart the server ... (It should be OK)

Why still not change [email protected]@@###&&&+++*** (What's going on, the server is broken, re-installed?) )


In fact, nothing is bad. You don't believe me? Look down.

Find a gadget, decompile the class file, and look for a reference constant, and you'll see for a moment.

It's not what you think.

Config.URLConfig.PAGE_NUM

But

"http://www.xxxx.com/" 10

See, constants are replaced with values directly in the class file.

That still want to use Config.java, do not want to change many places, how to do?

Two methods:

public class Config {public    static final string Url=new string ("http://www.xxxx.com/");//Useful public    static final int page_num=new Integer (10);//Bad use}
public class Config {public    static String GetURL () {return ' http://www.yyyy.com ';    }     public static int Getpagenum () {return;    }}


Well, the rest, waiting for everyone to find out.


What is the static constant in the class file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.