枚举作为Java中常量的替换

时间:2014-07-02 06:35:33

标签: java enums constants

我现在听说有一天我们应该使用枚举而不是常量。 在所有情况下都可以吗? 枚举是否替换常量

在下面的例子中,我在常量文件中定义了常量,而ConstantsTest使用它们

public final class Constants {

private Constants(){

}
public static final String ACCOUNT="Account";
public static final String EVENT_ITEM ="EventItem";
public static final int MULTIPLIER_ONE = 1;
public static final int MULTIPLIER_NEGATIVE_ONE = -1;
public static final String BALANCE_AFTER_MODIFICATION = "BalanceAfterModification";
public static final String COMMA = ",";
public static final String DOTPSV =".psv";
public static final String NEW_LINE = "\n";
}


 // Test Class
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ConstantsTest {
private static File rootDir = new File(".");    
public static void main(String[] args) throws IOException {

    Map<String,Integer> accountBalance = new HashMap<String, Integer>();
    accountBalance.put("123",55000);
    accountBalance.put("223",15000);
    writeToFile(Constants.ACCOUNT, accountBalance, true, 2000);
    // do operation 

 }

/**
 * 
 * @param fileType
 * @param inputData
 * @param add if true add balance  else substract the balance 
 * @return
 * @throws IOException
 */
 private static File writeToFile(String fileType , Map<String,Integer>accountBalance ,boolean add, int amount) throws IOException{
     File file = null; 
     FileWriter fw = null;
     try{
         if(Constants.ACCOUNT.equals(fileType)){
             file = new File(rootDir,Constants.ACCOUNT+Constants.DOTPSV);//creating a fileName using constants
             fw = new FileWriter(file);
             fw.write(Constants.ACCOUNT+Constants.COMMA+Constants.BALANCE_AFTER_MODIFICATION);//Writing Header in file using constant values
             updateBalance(accountBalance, add, amount);
             for(String key:accountBalance.keySet()){
                 fw.write(Constants.NEW_LINE);
                 fw.write(key+Constants.COMMA+accountBalance.get(key));
             }
         }
         else if(Constants.EVENT_ITEM.equals(fileType))
         {
             // write to EventItem.psv
         }
     } finally{
         if (null!=fw){
             fw.close();
         }
     }

     System.out.println("File created successfully");
     return file;

 }

private static void updateBalance(Map<String, Integer> accountBalance,
        boolean add, int amount) {
    for(String key:accountBalance.keySet()){
         int currentBal = accountBalance.get(key);
         if(add){
             accountBalance.put(key,currentBal+amount*Constants.MULTIPLIER_ONE); // do lot of calculations
         }else{
             accountBalance.put(key,currentBal+amount*Constants.MULTIPLIER_NEGATIVE_ONE);// do a lot of calculations
         }
     }
}

}

请在我的示例示例中建议枚举更好或者使用常量的当前方法

6 个答案:

答案 0 :(得分:4)

在您的特定情况下,使用枚举是经典的解决方案。

首先,让我们重新编写您的Constants作为枚举:

public enum Constants {
    ACCOUNT,
    EVENT_ITEM,
    ;

}

public enum Operation {
   MULTIPLIER_ONE {
        public int action(int value) {
            return value;
        }
   },
   MULTIPLIER_NEGATIVE_ONE {
        public int action(int value) {
            return value * (-1);
        }
   },
   ;
   private Operation(int coef) {
        this.coef = coef;
   }

   public abstract int action(int value);
}

现在而不是写作:

if(Constants.ACCOUNT.equals(fileType)){
} else if(....)

您可以使用switch/case甚至更好地定义:define方法(让我们将其称为action()到枚举中并从您的代码中调用它。请参阅上面Operation枚举中的示例。在这种情况下,您的代码变得微不足道:不再需要if/else或切换语句。一切都很简单。验证在编译时完成:您在枚举中定义了抽象方法,如果不为它实现此方法,则无法向枚举添加另一个元素。使用if/else结构维护时,这不会发生,这是程序员的责任。

我只知道枚举的一个限制:在注释中使用字符串常量。有许多带字符串属性的注释。例如XmlElement(name="foo")。即使你定义枚举

enum FooBar {
    foo, bar
}

你不能在注释中使用它:

@XmlElement(name=FooBar.foo) // is wrong because String type is required
@XmlElement(name=FooBar.foo.name()) // is wrong because annotations do not support method invocation 

在所有其他情况下,我更喜欢枚举。

答案 1 :(得分:2)

您应该使用枚举此代码

enum Constants {
  ACCOUNT,
  EVENT_ITEM ,
  COMMA ,
  DOTPSV ,
  BALANCE_AFTER_MODIFICATION ;

@Override
public String toString() {
  switch(this) {
    case ACCOUNT: return "Account";
    case EVENT_ITEM : return "EventItem";
    case COMMA : return ",";
    case DOTPSV : return ".psv";
    case BALANCE_AFTER_MODIFICATION : return "BalanceAfterModification";
    default: throw new IllegalArgumentException();
   }
 }
}

答案 2 :(得分:0)

Enum没有为使用它的班级定义contractinterface会这样做。使用Enum的类不是Enum类型。实现接口的类实际上与接口类型相同(接口是父接口...并且可以更改引用)。考虑到这些设计问题。告诉我,你的做法是否正确?

答案 3 :(得分:0)

对于提供的示例,常量会更好。接口变量默认为public static final。

public static final String ACCOUNT="Account";

请参阅Why are interface variables static and final by default?

答案 4 :(得分:0)

你得到枚举错误,不像你应该创建枚举而不是常量:枚举是与相关的常量的,例如:

enum Days {
    SUNDAY, MONDAY, TUESDAY, ...
}

来自 docs

  

枚举类型是一种特殊的数据类型,它允许变量   一组预定义的常量。

答案 5 :(得分:0)

只有我们可以将Enums用于单个组中的常量值。 让我们假设:周,月,颜色,性别,过程状态

使用单个枚举来存储所有常量并不是一个好主意。相反,我们可以为每组常量使用一个枚举。

让我们假设您保留了一些颜色代码,然后更好地使用Color枚举而不是保存为常量。