Annotation in Android

Balakrishnan
Tech Log
Published in
3 min readAug 20, 2018

Why Not Enum?

I did my research on Enum vs Annotation in android, to explore the myth ENUM is bad and I ended up here. There is not much of difference in using ENUM and Annotation ( IntDef and StringDef) at least not application wide.

But it is suggested in Android developer page to use Annotation rather than the ENUM because it consumes 2x of Constant and final elements. In real world application ENUM is not going to make much of differences.

If we are writing library which will be used across the app, we should avoid using ENUM as it affects memory and performance. Progaurd by default optimize the ENUM in into Constatans but this happens with certain restrictions and it dose not optimize the enum all the time.

Let us consider that we have enum named FRUITS

public enum FRUITS{   APPLE,   BANANA,   MANGO; }

Note that every item in ENUM is static and final values, which persist in memory from initial execution till the end of app. If we are about to use this ENUM in certain times in the Program the memory space will not be cleared util app is destroyed. Garbage collector cannot collect these values since these are static.

Decompiled class of enum looks like following.

public final class FRUITS extends java.lang.Enum<FRUITS>{ 
public static final FRUITS APPLE;
public static final FRUITS BANANA;
public static final FRUITS MANGO;
public static FRUITS[] values();
public static FRUITS valueOf(java.lang.String);
static {};
}

Annotation IntDef

Simple implementation for IntDef is as follows:

    @Retention(SOURCE)    @IntDef({NONE, ANDROID, IPHONE})    public @interface Frame {
public static final int NONE = 0;
public static final int ANDROID = 1;
public static final int IPHONE = 2;
}
@Frame
public static int currentMode = NONE;

@Frame
public static int getFrameMode() {
return currentMode;
}
public static void setFrameMode(@Frame int mode) {
currentMode = mode;
}

Retention Policy

Retention Policy are used in conjunction with the Retention meta-annotation type to specify how long annotations are to be retained.

SOURCE — Annotations are to be discarded by the compiler. Not available in de-compiled class.

CLASS — Annotations are to be recorded in the class file by the compiler but not retained by the VM at run time in other words the annotations cannot be inspected. This is default annotation policy

RUNTIME — Annotations are to be recorded in the class file by the compiler. retained by the VM at run time.These can retried in de-compiled class in other words the annotations can be inspected

  1. Frame is Annotation name declared by @ followed by interface and Annotation name
  2. Getter and Setter are optional and defined static for accessibility purposes

Use annotations in Application class to access anywhere in app, simple implementation of Application class as follows in link

Note : For StringDef Annotation use String instead of int

Reference link: https://youtu.be/Hzs6OBcvNQE , https://plus.google.com/+JakeWharton/posts/bTtjuFia5wm

If you have any questions or comments, please feel free to drop them in the comments section below or send me a tweet.

If you like this article, do give it a thumbs up, comment on it and share with with your friends.

--

--