ep3gpw
Last Updated: February 25, 2016
·
586
· wannabegeekboy
380123 2865223158521 1527694351 n

Avoid Enums Where You Only Need Ints

Enums are very convenient, but unfortunately can be painful when size and speed matter. For example, this:

public enum Shrubbery { GROUND, CRAWLING, HANGING }
adds 740 bytes to your .dex file compared to the equivalent class with three public static final ints. On first use, the class initializer invokes the method on objects representing each of the enumerated values. Each object gets its own static field, and the full set is stored in an array (a static field called "$VALUES"). That's a lot of code and data, just for three integers. Additionally, this:

Shrubbery shrub = Shrubbery.GROUND;
causes a static field lookup. If "GROUND" were a static final int, the compiler would treat it as a known constant and inline it.

Say Thanks
Respond
Awesome Job

20838750 ca12 11e7 8395 3d07316c018d
Overijssel,Amsterdam, Arnhem
·
Overijssel, Amsterdam, Arnhem
·
Full Time