Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.8.5
-
Fix Version/s: 1.8.6, 2.0-beta-3
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
As a side effect to GROOVY-5150, the class constant pool is used for non final fields too:
I have this class:
public class A { public static int i = 5; }
compile it with javac and you get the following related to i (using javap):
public static int i; static {}; Code: Stack=1, Locals=0, Args_size=0 0: iconst_5 1: putstatic #2; //Field i:I 4: return
Compile it with groovyc and these days I seem to get this:
public static int i; Constant value: int 5
Now I thought the 'constant value' attribute for field objects was for proper constants (i.e. final fields), not for initialization values. That seems to be what it indicates here:
===
4.7.2 The ConstantValue Attribute
The ConstantValue attribute is a fixed-length attribute used in the attributes table of the field_info (§4.5) structures. A ConstantValue attribute represents the value of a constant field that must be (explicitly or implicitly) static;
===
Indeed javac will only use that attribute if I make the field final.
Now the JVM doesn't seem to be particularly strict here and the static does get the right value initially and allow it to be changed (at least on the VM version I'm using, haven't tried any others). It just seems to be a slight abuse of the meaning of constant value. I guess I just wanted to check it was deliberate.
thanks,
Andy