When I read some open source java projects, I found that there are two ways to declare a static list variable with a default value in the class.
Is there any difference between these two methods? (performance, pros or cons)
- initialize Map object with all the initial values along with declaration:
public class OrderStatusHandler {
private static final Map<Integer, String> ORDER_STATUS = new HashMap<Integer, String>() {{
ORDER_STATUS.put(0, 'UNPAID');
ORDER_STATUS.put(1, 'PAID');
ORDER_STATUS.put(2, 'FINISH');
}};
...
}
- initialize List object with static block:
public class OrderStatusHandler {
private static final Map<Integer, String> ORDER_STATUS = new HashMap<Integer, String>();
static {
ORDER_STATUS.put(0, 'UNPAID');
ORDER_STATUS.put(1, 'PAID');
ORDER_STATUS.put(2, 'FINISH');
}
...
}