<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Ryan, is there a use case where people call getEnumConstants() and access a fixed index of array element?</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
This is so far the only use case I can think of that would be negatively impacted by the lack of constant folding for this method.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
In addition, I do not recommend adding more @Stable - we should consider better representations like LazyConstant.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Regards</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Ryan Hallock <ryan@hallock.us><br>
<b>Sent:</b> Thursday, February 12, 2026 3:45 PM<br>
<b>To:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Add @Stable to Class#enumConstants.</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hello, I am new to OpenJDK development and I want to propose changes to Class#enumConstants from Class#getEnumConstants()/Class#getSharedEnumConstants().<br>
<br>
Currently consider the following example:<br>
```java<br>
enum E {<br>
A, B, C<br>
}<br>
E[] values = E.values();<br>
E[] constants = E.class.getEnumConstants();<br>
assert Arrays.equals(values, constants);<br>
```<br>
These methods provide an equal array of values, but they are treated differently. This is because the _constants_ stored by Class#enumConstants are non final and declared volatile. So C2 can't trust this call. Consider if I only wanted to access the value from
an offset from these arrays.<br>
```java<br>
E value = values[0];<br>
E constant = constants[0];<br>
assert value == constant;<br>
```<br>
_constant_ would never be folded but _value_ would be. <br>
<br>
So I propose the addition of @Stable annotation added to Class#enumConstants so _constant_ can be folded by C2. This is not a trivial change as it requires changing the data race to abide by @Stable. Could I be sponsored for this change (JBS issue as well)?<br>
<br>
<div>A future proposal would be adding @Stable to Class#enumConstantDirectory, though that can be a seperate enhancement as that might require changing of the data structure to allow folding. Let me know if it should be combined as the Stable annotation itself
would be implemented in a similar way for both.</div>
<br>
Thanks,<br>
Ryan Hallock<br>
<br>
</div>
</div>
</body>
</html>