JDK-4777101 : final treatment of anonymous classes not marked in .class files?
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-11-11
  • Updated: 2016-07-07
  • Resolved: 2013-08-28
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Name: gm110360			Date: 11/11/2002

jdk 1.4.1 and earlier versions


jls states, that anonymous classes are implicit final.
But it seems, the class-files are not marked as such.
An anonymous class is compiled to a separate class-file
named like SomeClass$1 . When I open this class-file with
JBuilder, it is (somewhat) decompiled, but shows no 'final'
Nothing prevents me to compile and run something like:
 Object o = new SomeClass$1() {};
in another class in the same package.
I think, this is not intended. I don't have headache from
this - but I just want to be sure, that the VM can
immediatly use the fact, that an anonymous class (and it's
methods) is final, without too much 'hotspotting'.
Typical application of anonymous classes like comparators
would immediatley benefit from this.
I propose to explicitly mark class files compiled from
anonymous classes as 'final'.

This bug can be reproduced always.
(Review ID: 166886) 

Requiring ACC_FINAL to be set for anonymous classes will not happen. This policy stems from the prior Evaluation, and is reiterated in 6520152 and 7064370. As an aside, it appears that in JDK6, 6219964 caused ACC_FINAL to be set for those anonymous classes which are declared in a static context - this does not appear to have caused problems, but it should not be extended to all anonymous classes. Requiring ACC_FINAL to be unset is something I considered for JVMS 4.1, but rejected. It is the job of a compiler spec, not the JVM spec, to mandate that "A class file that represents an anonymous class of the Java language must not have its ACC_FINAL flag set." It's OK for JLS 15.9.5 to say "An anonymous class is always implicitly final". ACC_FINAL represents "_Declared_ final; no subclasses allowed", but 15.9.5 only says an anonymous class is "implicitly final", not "implicitly _declared_ final". Separately, the compiler ought to prevent "Object o = new SomeClass$1() {};" on the grounds that SomeClass$1 is an anonymous class (can be determined by its binary name alone), hence implicitly final, hence cannot be subclassed.

EVALUATION It seems very plausible that anonymous classes be marked final in the binary form. ###@###.### 2002-11-13 The "ACC_FINAL" flag is one component to the computation of the default serial version UID of a class. If I make this change, then the default serial version UID of every anonymous class in the universe will suddenly change. Are you sure you want that? I suggest instead we reword the JLS so it no longer implies this bit is set. ###@###.### 2002-11-13