JDK-8161009 : 15.9.5: Anonymous classes aren't actually 'final'
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7,8
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2016-07-07
  • Updated: 2018-08-03
  • Resolved: 2017-01-18
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
9Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The following is asserted about anonymous classes:

"An anonymous class is always implicitly final (ยง8.1.1.2)."

Longstanding behavior of javac, since 1.3, has been, for the most part, *not* to treat the classes as 'final'.  To address this inconsistency, the specification should be changed to accurately reflect the reference implementation.

Specifically, anonymous classes are almost never generated with the ACC_FINAL flag set.  We can't change this longstanding behavior without impacting some serialization clients (this would be permissible, but is unnecessarily disruptive).  And we can't faithfully implement Class.getModifers (which promises to provide the "Java language modifiers") without the class files encoding the language's modifiers.

Some history:
- JDK-4243945 observes a change that stopped setting ACC_FINAL in 1.3
- JDK-4777101 discussed changing the class files back to their 1.2 form, ultimately giving up due to compatibility concerns
- JDK-6219964 and JDK-6520152 observed that finality is observable at compile time via a cast, leading to an awkward compromise in which anonymous classes are considered 'final' temporarily during type checking
- JDK-8129576 once again observed the inconsistency between JLS 15.9.5 and 'Class.getModifiers'

The change impacts the set of legal programs, in that it allows some casts that would be considered illegal under the current specification (see JDK-6219964).  But, after searching for mentions of 'final' classes in JLS, I don't anticipate any other impact, meaning that this is a source-compatible fix.
Comments
Proposed change to 15.9.5 ("~~" means deletion, "**" means insertion/modification): An anonymous class is ~~always implicitly~~ **never** `final` (8.1.1.2). [Note:] **Even though an anonymous class is not `final`, it is impossible to declare a subclass of an anonymous class, because the anonymous class cannot be named.** ----- A related change to 6.7, clarifying that anonymous classes do not have names: A local **or anonymous** class does not have a fully qualified name. ... A local **or anonymous** class does not have a canonical name.
17-01-2017

Removing tck-red label (it is rarely acceptable for spec issues).
17-01-2017