When to use Enum / Int Constants

I have a question that when should we use Enum and when should we use a final constants?

I know that it has been discussed at Enums and Constants. Which to use when? though it is C# question.

My question is why Android use so many Constants rather than Enum? For example , Context

In my opinion, if we use constants, there may be the risk that as below: if we define a LEVEL Constant that

 public static final int LEVEL_LOW=1;
 public static final int LEVEL_MEDIUM=2;
 public static final int LEVEL_HIGH=3;

when we pass a param of int =4. it will not have compile error, and if we pass a number of 1, the code reader may not easily know what it means.

But Enum can solve this problem though it may cause more overhead since it is Object.

So why Android uses constants instead of Enum? Is there any principle that when should we use constants or Enum in such case?


This is related to android history. There were unconfirmed performance issues in versions before Froyo. It was recommended to not use enum by the developers. Since Froyo the Designing for Performance documentation was rewritten as described here.

As you may have noticed, we rewrote the Designing for Performance documentation for Froyo. Previously it was a bunch of stuff that may have been true at some point, but had long ceased to bear any relationship to reality. In Froyo, every single claim in the document is backed by a benchmark to prove (or, in future, disprove) it. You can peruse the "Designing For Performance" benchmarks in your browser.

But there was no point in changing the structure of legacy content.

The performance can be related to having String required to be stored. There is significant difference between the creation of a single class for every constants vs. multiple enums.

For example in Java 7 when you have a enum with two fields you need 44 items in poll constant and for a class with two static final integers you need only 17.

What is the difference

class ContantField {
  public static final int f1 = 0;
  public static final int f2 = 1;
}

enum ContantEnum {
  E1,E2
}

This two declarations are very different in the way there are stored and used. The simplification of ContantEnum could look like

class ContantEnum {
   public static final Enum enum0    = new Enum("V1",0);
   public static final Enum enum1    = new Enum("V2",1);
   public static final Enum[] values = new Enum[] {enum0,enum1};
} 

By this simplification you can notice that enum require more memory resources than int .

To answer your question, it must be understood the role of enums. One role of enum is to increase compile time type safety.

To point that out see this example:

public void setImportantThing(int priviledge, int rights)

public void setImportantThing(Privilege p, Right r)

In the case of int we can pass any value that is an int. In he tcase of enum we are forced to use the proper one.

The case we have here is trade off between compile time validation and memory usage on runtime. You should decide for yourself when you should use enum and where static int is sufficiently secure.

Note: enum was introduced to Java in version 1.5, using them before this was quite problematic more.

In Android Studio Beta, the developer will be able to enforce type safety using annotation.


Enums are:

  • Safer - more resilient to change.

    A change to the list of enums is more likely to cause compile-time errors if the change was mistaken.

  • Clearer - most developers will instantly understand that the items are connected in some way.

    enum { A, B, C } is much more obviously a group of items with a connection than psfi A = 0; psfi B = 1; psfi C = 2; psfi A = 0; psfi B = 1; psfi C = 2;

  • So unless you have a measurable advantage to using public static final int , be it in memory footprint or speed, you should always use enum .

    See When is optimisation premature?


    Basically the Android core is C/C++ code. This works with integers. So when using enums in Java every value had to be "translated". This would cost CPU time and memory. Both are rare on embedded systems.

    链接地址: http://www.djcxy.com/p/39578.html

    上一篇: C ++ STL容器和中

    下一篇: 何时使用Enum / Int常量