How to refactor constants to enums in Eclipse?

前端 未结 2 1258
时光取名叫无心
时光取名叫无心 2021-01-11 15:33

How do I refactor Java constants to enums with eclipse?

I found no built-in functionality in eclipse: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt

2条回答
  •  日久生厌
    2021-01-11 16:17

    Refactoring never changes the "external behavior"!

    For example:

    public enum Test {
        NAME1("abc");
    
        Test(String name) {}
    
        public static final String _NAME1="abc";
        public static void main(String[] args) {
            String k = Test._NAME1;
            String m = Test.NAME1;
        }
    }
    

    If you refactor _NAME1 to NAME1 (enum) ,the code crashes on instantiation of m. The refactoring would never be successful!

提交回复
热议问题