What Is an Enum?
A variable can be an enum type, which is a specific data type that allows it to be a set of specified constants.
Enums in Java can be thought of as classes with a set of constants. The Java enum constants are inherently static and final.
We utilise enums to define our own data types, similar to classes.
Why do we use ENUM ?
When a variable can only take one of a small number of possible values, we always use Enums.
For example:
enum Color {
AMARANTH,
VERMILION,
GAMBOGE;
}
We usually utilise ENUM instead of Integer or String constants to boost compile time checks and eliminate issues caused by sending in erroneous constants.
Disadvantages of using ENUM
When compared to an Integer or String constant, Enum values use more memory.
Adding a single ENUM increases the size of the final DEX file by 13 times the Integer constant. It also causes a problem of runtime overhead, which means your software will take up extra space.
We may not even be aware that the enum is affecting our app’s performance. Writing a couple of enum classes might not have a big influence on our project. However, if you’re utilising some enum classes and your app’s libraries are using others, merging all of these enums could have a significant influence on your app, perhaps increasing the file size and affecting speed.
As a result, excessive use of ENUM on Android will increase the size of the DEX and the size of the runtime memory allocation.
If your programme uses a lot of ENUM, you should utilise Integer or String constants instead.
How to Overcome this problem?
You can use the android.support.annotation package to access the annotation library, which includes TypeDef annotations. These annotations ensure that a parameter, return value, or field refers to a specified set of constants.
They also allow code completion to present the allowable constants automatically.
Instead of Enum, IntDef and StringDef are some Amazing Constant Annotations that can be utilised. These annotations will assist us in checking variable assignments during build time, such as Enum.
How to use it?
Let’s look at a simple example to see how it works.. Below are code snippets of a ConstantColor.class.
With a set of int constants, we have a ConstantColor class. As a result, we must pass the value of the int type from the available set when creating the object. However, I was passing a value that wasn’t in the set of constants in my main method, which may lead to an issue.
The code snippet below has no type safety.
public class ConstantColor
{
public static final int AMARANTH = 0;
public static final int VERMILION = 1;
public static final int GAMBOGE = 2;
public ConstantColor(int pObjColor){ .
System.out.println("Colour selected is:" + pObjColor)
}
// Driver method
public static void main(String[] args)
{
ConstantColor lObjColor1 = new ConstantColor(0);
System.out.println(lObjColor1);
}
}
The following is a comparable enum-based code implementation:
public class ConstantColor
{
enum Color {
AMARANTH,
VERMILION,
GAMBOGE;
}
public EnumColor(Color pObjColor){ .
System.out.println("Colour selected is:" + pObjColor)
}
// Driver method
public static void main(String[] args)
{
ConstantColor lObjColor1 = new ConstantColor(EnumColor.AMARANTH);
System.out.println(lObjColor1);
}
}
At the build.gradle level for the app, add the support-annotations dependency:
dependencies {
implementation 'com.android.support:support-annotations:28.0.0'
}
Declare the constants and @IntDef for these constants. The new enumerated annotation type is declared using @interface in Typedef annotations. The annotations @IntDef and @StringDef, as well as @Retention, annotate the new annotation and are required to define the enumerated type.
The annotation @Retention(RetentionPolicy.SOURCE) instructs the compiler not to save the enumeration annotation data in the.class file.
public class ConstantColor
{
public static final int AMARANTH = 0;
public static final int VERMILION = 1;
public static final int GAMBOGE = 2;
public ConstantColor(@Color int pObjColor) {
System.out.println("Color Selected is :" + pObjColor);
}
@IntDef({AMARANTH, VERMILION, GAMBOGE})
@Retention(RetentionPolicy.SOURCE)
public @interface Color {}
public ConstantColor(int pObjColor){ .
System.out.println("Colour selected is:" + pObjColor)
}
// Driver method
public static void main(String[] args)
{
ConstantColor lObjColor1 = new ConstantColor(AMARANTH);
System.out.println(lObjColor1);
}
}
Conclusion
Enums add at least two times the number of bytes to the entire APK size and can require 5-10 times the amount of RAM as similar constants.
Avoid using enums in your code if you want your project to be memory and performance efficient.