0

I've used quite a few Java libraries / APIs and noticed static final's are used everywhere, for example within Android.

Why is this? I know you can quickly | flags together, which can be useful, but you can equally use an EnumSet.

Another problem I have especially with android is there being loads of unscoped flags, which would be cleaner with enums. For example

You also then can't confuse a set of enums from one class with another, as you can with static final ints

Tobiq
  • 2,489
  • 19
  • 38
  • Possible duplicate of https://stackoverflow.com/q/22596495/4288506 – Marcono1234 Apr 14 '19 at 19:19
  • Possible duplicate of [Justification for using a bitfield instead of EnumSet in modern Java 8 API](https://stackoverflow.com/questions/22596495/justification-for-using-a-bitfield-instead-of-enumset-in-modern-java-8-api) – Security Hound Jun 03 '19 at 20:55

1 Answers1

3

I have not found an official statement by the Android project, but the reason most probably is: performance, specifically memory usage.

Enums (because they are Java classes) require more memory than integer constants. Even the official Android docs (used to) warn against using them:

For example, enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

Android Developer Documentation, "Manage Your App's Memory", as of January 2017

Interestingly enough, the current version of this page (as of April 2019) no longer mentions Enums. Maybe the problem is less relevant today.


Note that the Android SDK offers an alternative to enums, which also gives you some amount of compile-time safety: the annotation @IntDef. That seems to be what you are supposed to use instead of enums.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • I have a hard time believing a lot of app developers have read that documentation at anytime in history. Interesting nevertheless – Darren Forsythe Apr 14 '19 at 01:40
  • @DarrenForsythe: I would certainly hope app developers read the official documentation, specifically the guides (which are not that long). But yes, many people probably just muddle through. Also, if your app performs well for the intended audience, there is usually no need to worry about performance (premature optimization and all that...). At any rate, this question is about the SDK itself, and I certainly hope that the developers of the Android SDK read their own docs. – sleske Apr 14 '19 at 11:16
  • This question Isn't solely about android. I've used other libraries that incorporate the same usage, but android is the most well-known. – Tobiq Apr 14 '19 at 13:48
  • @Tobiq: A comment is not the place for that information; please edit your question to indicate this. Right now, the question is a bit unclear whether it is asking specifically about Android, or in general. – sleske Apr 16 '19 at 08:23
  • The question never implied it was solely about android. Which is why I left this comment on your post. As I said, I used android as an example because many people are familiar with the case, there. But the first line of the question says `I've used quite a few Java libraries / APIs and noticed static final's are used everywhere` – Tobiq Apr 16 '19 at 10:14