Anonymous-Inner classes showing incorrect modifier

前端 未结 4 1901
半阙折子戏
半阙折子戏 2020-11-30 11:10

To my understanding, the following code should have printed true as output.

However, when I ran this code it is printing false.

Fro

相关标签:
4条回答
  • 2020-11-30 11:21

    An anonymous class is never final (§8.1.1.2).

    JLS 11 - 15.9.5. Anonymous Class Declarations

    I didn't know the reasoning behind this, but, according to @Hulk's answer and this bug report, it seems the specification of previous versions slightly misled us saying that anonymous classes are final.

    0 讨论(0)
  • 2020-11-30 11:36

    Note that the wording in the JLS of that particular section has changed significantly since then. It now (JLS 11) reads:

    15.9.5. Anonymous Class Declarations:

    An anonymous class is never final (§8.1.1.2).

    The fact that an anonymous class is not final is relevant in casting, in particular the narrowing reference conversion allowed for the cast operator (§5.5). It is also of interest in subclassing, in that it is impossible to declare a subclass of an anonymous class, despite an anonymous class being non-final, because an anonymous class cannot be named by an extends clause (§8.1.4).

    This change in wording was introduced in JLS 9. The semantics of anonymous classes and the behavior of the methods in the question remained mostly unchanged, the intention was to avoid exactly the kind of confusion this question is about.

    The ticket that caused the change says:

    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.

    However, the change did actually change semantics to some degree, and this was also documented in this ticket as an acceptable impact:

    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.

    0 讨论(0)
  • 2020-11-30 11:36

    Anonymous classes are considered implicitly final since you can't create sub-classes of them. That doesn't mean that the Modifier.FINAL modifier should be set for anonymous classes.

    0 讨论(0)
  • 2020-11-30 11:36

    See Javadoc of Class.getModifiers(): https://docs.oracle.com/javase/10/docs/api/java/lang/Class.html#getModifiers()

    It says "...The values of its other modifiers are not determined by this specification".

    0 讨论(0)
提交回复
热议问题