zoukankan      html  css  js  c++  java
  • 静态常量(static final)在class文件里是如何的呢?

    近期写项目遇到一个问题,来回折腾了几次,最终探究清楚了。不废话。上样例。


    背景:由于项目小,没有使用配置文件,全部静态常量都放在Config.java里面了

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


    看起来也不错啊,没什么大问题。都这么用的啊。

    好吧,放到server上执行一下,正常啊。


    接下来,问题来了。

    URL改,PAGE_NUM也改。

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

    好了,生成下class文件,放到server上。喝杯茶。。。

    刷新下,没变。再等等。server有个转换时间。。。

    30s。

    。。没变

    1min。。。没变

    重新启动server。

    。(应该好了)

    怎么还是没变???~~~!!!@@@###&&&+++***(究竟怎么回事。server坏了。重装?)


    事实上什么都没坏。你不信?往下看。

    找个小工具。把class文件反编译一下,找找引用常量的地方。你就瞬间明确了。

    不是你想象的

    Config.URL
    Config.PAGE_NUM

    
    而是
    

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

    
    明确了吧,常量在class文件里,直接替换为值了。
    

    那还是想用Config.java,又不想改非常多地方。怎么办?

    两个方法:

    public class Config {
        public static final String URL=new String("http://www.xxxx.com/");//好用
        public static final int PAGE_NUM=new Integer(10);//不好用
    }
    public class Config {
        public static String getURL(){
    	return "http://www.yyyy.com";
        } 
        public static int getPageNum(){
    	return 200;
        } 
    }


    好了,其余的,等待大家去发现。


  • 相关阅读:
    455. Assign Cookies
    [leetcode]Linked List Cycle
    *[topcoder]GooseTattarrattatDiv1
    [topcoder]FoxAndChess
    *[topcoder]TheTree
    *[topcoder]LittleElephantAndBalls
    *[topcoder]HexagonalBoard
    *[topcoder]AstronomicalRecords
    *[topcoder]LittleElephantAndIntervalsDiv1
    [topcoder]IncrementAndDoubling
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5105234.html
Copyright © 2011-2022 走看看