I came across a behavior, I didn't knew of earlier, in the follow up code.
Consider the 1st case:
public static void main(String[] args) {
final String str = null;
System.out.println(str.length()); // Compiler Warning: NullPointerAccess
}
As expected, the compiler shows me following warning on str
being null - Null pointer access: The variable str can only be null at this location.
Now, when I moved that variable a static final field initialized to null:
class Demo {
static final String str = null;
public static void main(String[] args) {
System.out.println(str.length()); // No Compiler Warning
}
}
Now, compiler doesn't show any warning. AFAIK, Compiler should know that str
being final, will not change its value, at any point of the code. And given that it is null
, will definitely result in NullPointerException
later on, which it does.
Although, compiler successfully warns me of this in the first case, why it cannot identify this in the 2nd case. Why this change of behaviour? The behaviour is same, if I change static
field to instance
field, and access it using an instance of Demo
.
I thought this behaviour might have been specified in JLS, so I went through the topic Definite Assignment, but didn't find anything related to this issue. Can anyone explain the change in behaviour? I'm looking for some strong point with some link to JLS if possible?
Apart from that, why compiler shows me only warning in the first place, as I think for the same reason I stated above, the method invocation will definitely throw NPE at runtime, since the field can't be changed? Why didn't it rather show me a Compiler Error? Am I expecting too much from compiler, as it seems to be quite obvious, that the runtime result of str.length()
can't be anything than NPE
?
Sorry for missing that earlier:
I'm using Eclipse Juno, on Ubuntu 12.04 with OpenJDK 7.