---
# System prepended metadata

title: Enums vs @IntDef
tags: [Android Dependency]

---

# Enums vs @IntDef
###### tags: `Android Dependency`
![](https://i.imgur.com/YlSaKzu.png)

[TOC]
# What are Enums?
They are used as items enumeration. However, we have to be aware that they classes and they take a lot of space in dex space after compiling, leaving less space for 
## Disadvantages
Require ***more than twice of memory*** in comparison to constants. Do not use it unless necessary. 


>[Why Enum costs so much? Click Here to check out video!](https://www.youtube.com/watch?v=Hzs6OBcvNQE&ab_channel=AndroidDevelopers)[color=lightblue]

## Advantage
Due to ART(**A**ndroid **R**un**T**ime) improvement, dex space where enums memory is stored also became possible to shrink considerably. And taking into account that the devices now has much more RAM than before, ++it is not actually affected too much++.

# What is @IntDef?
@IntDef, as well as @StringDef annotations, can **create enumerated annotations** of inter 

## How to use it?

### 1. Add Dependency
```java=
implementation 'com.android.support:support-annotations:22.0.0'
```

### 2. Add static constants
**Note:** Constants cost lower than enums.
```java=
public static final int NETWORK_ERROR = 0x1;
public static final int HTTP_ERROR = 0x2;
public static final int JSON_ERROR = 0x3;
public static final int UNKNOWN_ERROR = 0x4;
public static final int RUNTIME_ERROR = 0x5;
public static final int UNKNOWN_HOST_ERROR = 0x6;
```

### 3. Set up @IntDef & @Retention
By using **@IntDef** we can assign them as annotations in:
* Variables
* Methods return type
* Parameters: If use any other than the ones mentioned in CodeException then it will not pass when writing the method call.

By using @Retention, it tells the compiler not to store annotation data in the .class file.

**Example**
```java=
package com.zc.androiddependencies.httpbase;

import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class CodeException {

    public static final int NETWORK_ERROR = 0x1;
    public static final int HTTP_ERROR = 0x2;
    public static final int JSON_ERROR = 0x3;
    public static final int UNKNOWN_ERROR = 0x4;
    public static final int RUNTIME_ERROR = 0x5;
    public static final int UNKNOWN_HOST_ERROR = 0x6;

    @IntDef({NETWORK_ERROR, HTTP_ERROR, JSON_ERROR, UNKNOWN_ERROR, RUNTIME_ERROR, UNKNOWN_HOST_ERROR})
    //Tell the compiler not to store annotation data in the .class file
    @Retention(RetentionPolicy.SOURCE)
    //Declares CodeError annotation
    public @interface CodeError {}

    //Usage Example
    @CodeError
    private static int error;

    @CodeError
    public static int getError(){
        return error;
    }

    public static void setNetworkError(@CodeError int newError){
        error = newError;
    }
}

```

# Reference

* https://medium.com/default-to-open/android-then-and-now-intro-intdef-enums-bca22d5cca56
* https://riptutorial.com/android/example/15758/intdef-annotations