possible inconsistency for non-final static fields in JVMS

Alex Buckley alex.buckley at oracle.com
Fri Jul 31 17:21:39 UTC 2020

On 7/31/2020 7:29 AM, Sebastian Sickelmann wrote:
> When you generate a class and create a non-final static field that
> uses ConstantValue Attribute for initialization with no further
> initialization code in the static-initializing-code-block. It seems
> that the field is initialized as specified by 4.7.2.
JVMS 5.5 is clear that only final static fields are auto-initialized by 
a JVM implementation based on their ConstantValue attributes. However, I 
can believe that HotSpot also auto-initializes non-final static fields 
based on ConstantValue. This is because JVMS 4.7.2 conceived of 
ConstantValue as storing values for "constant fields" -- an undefined 
term, though we may suppose it meant "final static fields" -- but merely 
required a field to be static in order to receive auto-initialization 
based on ConstantValue. I detected this and other incongruities in how 
ConstantValue is used in JDK-8013650, but the thrust of that bug was the 
JLS, not the JVMS; I did not want to risk incompatibilities by 
tightening JVMS 4.7.2 to require a field be static _and final_ field to 
receive auto-initialization.

> What is the desired behavior that should be documented? Is it 
> feasible to use the ConstantValue Attribute for non-final fields? Or
> should we create static initialization instructions for non-final 
> static fields, Like javac? How should a Compiler / ClassInitializer /
> ClassVerifier react to non-final static fields with ConstantValue
> Attribute? >
> I personally like it the way I observed it:
> * ConstantValue Attribute can also used for non-final fields at the field is initialized with the value.
> * javac compiler creates access instructions instead of copiying the value into the other class.
> I was only confused by the intructions generated by ECJ.
> What do you think?
> Should we clarify it in the JVMS, or is there a chapter I did not found?

You should rely primarily on JVMS 5.5 -- only final static fields are 
auto-initialized. ConstantValue may be emitted by a compiler to store 
the value of a non-final static field, or the value of a final 
non-static field, but those are not JVMS-defined topics.


More information about the discuss mailing list