<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>What you are proposing is not backward compatible,</div><div>i'm sure there are existing enum classes that declare a field named SIZE.</div><div><br data-mce-bogus="1"></div><div>And there is a simple solution, use an unmodifiable list.</div><div>For example:</div><div><br data-mce-bogus="1"></div><div>enum MyEnum {</div><div> public static final List<MyEnum> VALUES = List.of(MyEnum.values());</div><div>}</div><div><br data-mce-bogus="1"></div><div>here, MyEnum.VALUES.size() is a constant.</div><div><br data-mce-bogus="1"></div><div>regards,</div><div>Rémi</div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Turan Suleyman" <turansuleyman@proton.me><br><b>To: </b>"compiler-dev" <compiler-dev@openjdk.java.net><br><b>Sent: </b>Wednesday, November 19, 2025 12:04:21 AM<br><b>Subject: </b>[JEP Proposal] Constant-Time Enum Size Access (https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65)<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div style="font-family: Arial, sans-serif; font-size: 14px;"></div><span style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Hi all,</span><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">I’d like to start a discussion/gather feedback about a "small" quality of life enhancement to <code><b>java.lang.Enum</b></code> to add a constant-time, allocation-free way to obtain the number of constants in an enum type.</div><div style="text-align: left; margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>Today, the usual approach is: </span><span style="white-space-collapse:preserve"><i><b>int n = MyEnum.values().length;</b></i></span></div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">This creates a <b>new array</b> on every call, which is both <b>non-constant time</b> and <b>allocates memory unnecessarily</b>.</div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>Since the number of enum constants is fixed and known at compile time, this information could be exposed more efficiently.</span></div><div style="text-align: left; margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>I’ve written a draft JEP describing two possible designs:</span></div><ol style="text-align: left; background-color: rgb(255, 255, 255);"><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">A <b>compiler-generated static constant</b>, e.g.<br><code>public static final int SIZE = <number of constants>;</code></div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">A <b>built-in API method</b> such as<br><code>MyEnum.size();</code></div></li></ol><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Draft JEP: <a href="https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65" title="https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65" style="margin:0px" target="_blank" rel="noreferrer nofollow noopener">https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65</a><br data-mce-bogus="1"></div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">I’d appreciate feedback on:</div><ul style="text-align: left; background-color: rgb(255, 255, 255);"><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">Whether this seems like a worthwhile enhancement</div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">Preference between the two design options</div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">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.</div></li></ul><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Thank you for your time and thoughts.</div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Best regards,</div><span style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><b>Turan</b></span><div style="font-family: Arial, sans-serif; font-size: 14px;" class="protonmail_signature_block"><div class="protonmail_signature_block-proton">
</div>
</div><br></blockquote></div></div></body></html>