JDK-8020216 : javac, compile time error isn't shown when final static field is not assigned
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u45
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • Submitted: 2013-06-29
  • Updated: 2014-09-23
  • Resolved: 2013-12-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b01Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
javac -version
javac 1.6.0_25

java -version
java version  " 1.7.0 " 
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
ver -  5.1.2600 Service Pack 2 Build 2600
ver-  6.1.7601 Service Pack 1 Build 7601

A DESCRIPTION OF THE PROBLEM :
If a blank final class variable in a class is not initialized and the constructor of that class is not allowed to complete by making it to throw an Exception,  before it could be completed, the program compiles successfully.

Although, it is mentioned in JLS (JLS8.3.1.2) that :
It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.

REGRESSION.  Last worked in version 6u45

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Declare a blank final static field in a class.
2. Make the constructor of that class to throw an Exception.
3. Compile the class file on command prompt .

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation should be unsuccessful. It should show an error : variable i might not have been initialized
ACTUAL -
Compilation is successful.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Main
{
    private final static int i ;
    public Main()throws InstantiationException
    {
        throw new InstantiationException( " Can't instantiate " );
    }
}


---------- END SOURCE ----------

SUPPORT :
YES
Comments
JLS 8.3.1.2 says "at the end of every constructor", but that necessarily refers more specifically to normal completion of the constructor. Abnormal completion *must* be allowed to leave the object in an uninitialized state. Consider this simple example: class HasBlankF { final int hc; HasBlankF(Object x) { //if (x == null) throw new NullPointerException(); hc = x.hashCode(); } } Whether or not the 'if' line is commented out, the call to hashCode can terminate abnormally and throw out of the constructor. This clearly cannot be treated as a compile-time error. It would (to put it mildly) surprise many Java programmers.
08-08-2013

javac should not accept this program. The JLS rule about blank final class variables being definitely assigned by a static initializer is independent of whether constructor bodies complete abruptly. javac also wrongly accepts the program if i is a blank final instance variable, despite JLS 8.3.1.2 requiring that such a variable is definitely assigned by the end of every constructor. It looks like the abrupt completion of the constructor is short-circuiting the definite assignment analysis.
11-07-2013