[JEP Proposal] Constant-Time Enum Size Access (https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65)
Turan Suleyman
turansuleyman at proton.me
Tue Nov 18 23:04:21 UTC 2025
Hi all,
I’d like to start a discussion/gather feedback about a "small" quality of life enhancement to java.lang.Enum to add a constant-time, allocation-free way to obtain the number of constants in an enum type.
Today, the usual approach is: int n = MyEnum.values().length;
This creates a new array on every call, which is both non-constant time and allocates memory unnecessarily.
Since the number of enum constants is fixed and known at compile time, this information could be exposed more efficiently.
I’ve written a draft JEP describing two possible designs:
- A compiler-generated static constant, e.g.
public static final int SIZE = <number of constants>;
- A built-in API method such as
MyEnum.size();
Draft JEP: https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65
I’d appreciate feedback on:
- Whether this seems like a worthwhile enhancement
- Preference between the two design options
- Whether anyone would be interested in sponsoring the JEP ( or rolling the change into an open piece of work) if there’s agreement to pursue it.
Thank you for your time and thoughts.
Best regards,Turan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20251118/0b69eee2/attachment.htm>
More information about the compiler-dev
mailing list