possible inconsistency for non-final static fields in JVMS
sebastian.sickelmann at gmx.de
Fri Jul 31 14:29:06 UTC 2020
Hi, I am not sure if I oversee something but from my point of view it looks like that there is a small inconsistency regarding non-final static fields.
The paragraphs in question are:
4.7.2. The ConstantValue Attribute
The ConstantValue attribute is a fixed-length attribute in the attributes table of a field_info structure (§4.5). A ConstantValue attribute represents the value of a constant field. There can be no more than one ConstantValue attribute in the attributes table of a given field_info structure. If the field is static (that is, the ACC_STATIC flag (Table 4.4) in the access_flags item of the field_info structure is set) then the constant field represented by the field_info structure is assigned the value referenced by its ConstantValue attribute as part of the initialization of the class or interface declaring the constant field (§5.5). This occurs prior to the invocation of the class or interface initialization method (§2.9) of that class or interface.
6. ......... Then, initialize each final static field of C with the constant value in its ConstantValue attribute (§4.7.2), in the order the fields appear in the ClassFile structure.
When you generate a class and create a non-final static fields 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 sprecified by 4.7.2. Also javac seems to create access instructions when it compiles other sources that accessed the field. (Btw. Eclipse ECJ does not, which is the reason i looked it up in the spec. It just copies the value as it would be static and final)
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?
More information about the discuss