Usually, constant variables are static, e.g.
static final int x = 1;
JLS guarantees[1] that nobody can observe its default value `0`. Practically speaking, static constant variables don't really have default values; JVM implementations do not need to zero these fields when the class is initialized.
--
Now, the funny thing is, an instance variable can also be a constant variable [2].
final int x = 1;
Obviously, it doesn't make sense to include a constant field per instance; the field ought to be static. But there might be a reason why the programmer chooses to make it an instance field at this point of time.
Anyways, the field is initialized in textual order; the default value `0` can be observed, for example, by reflections.
However, I don't see any non-reflection way of observing the default value `0`. The following program will print `1`
class Test
{
{
System.out.println(this.x); // x==0 here, if read by reflection
}
final int x = 1;
}
In fact, any `expr.x` will evaluate to `1`, as long as the type of `expr` is `Test`. (Though, if `expr` is null, javac diligently triggers a NullPointerException.)
This makes perfect sense. However, JLS only considers the simple name `x` as a constant expression [3]; `this.x` is not a constant expression, and it should not be inlined as `1`. So it's a javac bug. But we could also blame JLS here for being incomplete.
[1]
http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.3.2[2]
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4[3]
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28--
Zhong Yu