Should I strictly avoid using enums on Android?

后端 未结 7 1876
不知归路
不知归路 2020-11-29 15:36

I used to define a set of related constants like Bundle keys together in an interface like below:

public interface From{
    String LOGIN_SCREEN         


        
相关标签:
7条回答
  • 2020-11-29 16:25

    Use enum when you need its features. Don't avoid it strictly.

    Java enum is more powerful, but if you don't need its features, use constants, they occupy less space and they can be primitive itself.

    When to use enum:

    • type checking - you can accept only listed values, and they are not continuous (see below what I call continuous here)
    • method overloading - every enum constant has its own implementation of a method

      public enum UnitConverter{
          METERS{
              @Override
              public double toMiles(final double meters){
                  return meters * 0.00062137D;
              }
      
              @Override
              public double toMeters(final double meters){
                  return meters;
              }
          },
          MILES{
              @Override
              public double toMiles(final double miles){
                  return miles;
              }
      
              @Override
              public double toMeters(final double miles){
                  return miles / 0.00062137D;
              }
          };
      
          public abstract double toMiles(double unit);
          public abstract double toMeters(double unit);
      }
      
    • more data - your one constant contains more than one information that cannot be put in one variable

    • complicated data - your constant need methods to operate on the data

    When not to use enum:

    • you can accept all values of one type, and your constants contain only these most used
    • you can accept continuous data

      public class Month{
          public static final int JANUARY = 1;
          public static final int FEBRUARY = 2;
          public static final int MARCH = 3;
          ...
      
          public static String getName(final int month){
              if(month <= 0 || month > 12){
                  throw new IllegalArgumentException("Invalid month number: " + month);
              }
      
              ...
          }
      }
      
    • for names (like in your example)
    • for everything else that really doesn't need an enum

    Enums occupy more space

    • a single reference to an enum constant occupies 4 bytes
    • every enum constant occupies space that is a sum of its fields' sizes aligned to 8 bytes + overhead of the object
    • the enum class itself occupies some space

    Constants occupy less space

    • a constant doesn't have a reference so it's a pure data (even if it's a reference, then enum instance would be a reference to another reference)
    • constants may be added to an existing class - it's not necessary to add another class
    • constants may be inlined; it brings extended compile-time features (such as null checking, finding dead code etc.)
    0 讨论(0)
提交回复
热议问题