Dear Astha,
The reason is
Its not a compile time constant. A constant can be only of primitive type or
String. Integer doesn't fit in it. See
JLS 15.28
for definition of compile time constants. Basically in this Integer var is a reference type which is referring to an object of type INT.
I would like to add other stuff as well. Non constant fields are initialized in constructors. Static fields in static constructors and instance fields in instance constructors. For your case,
try making variable of type Integer and view bytecode:
ss staticFinalDemo1 {
static java.lang.Integer var;
static {};
Code:
0: bipush 100
2: invokestatic #5 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: putstatic #6 // Field var:Ljava/lang/Integer;
8: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #7 // String Static Initializer
13: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
16: return
}
on lines 0 - 5, value to an var field is assigned. bipush loads 100 to the stack, invokestatic creates Integer object and putstatic saves it to a static variable. In case of int type these lines are absent. Value already exists in constant pool.
HTH,
Ben