Anonymous inner classes showing unwanted modifier












13














To my understanding following code should print True.



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



From Java docs of Anonymous Classes 15.9.5. :




An anonymous class is always implicitly final




public class Test {
public static void main(String args) {
Object o = new Object() {
};
System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers()));
}
}


Can some one please help me understand this behavior.










share|improve this question



























    13














    To my understanding following code should print True.



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



    From Java docs of Anonymous Classes 15.9.5. :




    An anonymous class is always implicitly final




    public class Test {
    public static void main(String args) {
    Object o = new Object() {
    };
    System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers()));
    }
    }


    Can some one please help me understand this behavior.










    share|improve this question

























      13












      13








      13


      1





      To my understanding following code should print True.



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



      From Java docs of Anonymous Classes 15.9.5. :




      An anonymous class is always implicitly final




      public class Test {
      public static void main(String args) {
      Object o = new Object() {
      };
      System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers()));
      }
      }


      Can some one please help me understand this behavior.










      share|improve this question













      To my understanding following code should print True.



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



      From Java docs of Anonymous Classes 15.9.5. :




      An anonymous class is always implicitly final




      public class Test {
      public static void main(String args) {
      Object o = new Object() {
      };
      System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers()));
      }
      }


      Can some one please help me understand this behavior.







      java anonymous-class






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 51 mins ago









      Show Stopper

      4,8081861




      4,8081861
























          4 Answers
          4






          active

          oldest

          votes


















          9














          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 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.







          share|improve this answer































            4














            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.






            share|improve this answer





























              3















              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.






              share|improve this answer



















              • 1




                You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                – Michael
                36 mins ago





















              0














              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".






              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019203%2fanonymous-inner-classes-showing-unwanted-modifier%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                9














                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 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.







                share|improve this answer




























                  9














                  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 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.







                  share|improve this answer


























                    9












                    9








                    9






                    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 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.







                    share|improve this answer














                    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 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.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 19 mins ago

























                    answered 38 mins ago









                    Hulk

                    2,81311636




                    2,81311636

























                        4














                        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.






                        share|improve this answer


























                          4














                          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.






                          share|improve this answer
























                            4












                            4








                            4






                            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.






                            share|improve this answer












                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 46 mins ago









                            Eran

                            280k37452538




                            280k37452538























                                3















                                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.






                                share|improve this answer



















                                • 1




                                  You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                  – Michael
                                  36 mins ago


















                                3















                                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.






                                share|improve this answer



















                                • 1




                                  You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                  – Michael
                                  36 mins ago
















                                3












                                3








                                3







                                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.






                                share|improve this answer















                                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.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 5 mins ago

























                                answered 39 mins ago









                                Andrew Tobilko

                                26.2k104284




                                26.2k104284








                                • 1




                                  You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                  – Michael
                                  36 mins ago
















                                • 1




                                  You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                  – Michael
                                  36 mins ago










                                1




                                1




                                You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                – Michael
                                36 mins ago






                                You've linked to exactly the same section as the OP, but a more up-to-date version of the JLS. Perhaps it is worth describing why the discrepancy exists between the two versions rather than simply quoting the newer one.
                                – Michael
                                36 mins ago













                                0














                                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".






                                share|improve this answer


























                                  0














                                  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".






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    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".






                                    share|improve this answer












                                    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".







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 21 mins ago









                                    Prasad Karunagoda

                                    42936




                                    42936






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019203%2fanonymous-inner-classes-showing-unwanted-modifier%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Eastern Orthodox Church

                                        Zagreb

                                        Understanding the information contained in the Deep Space Network XML data?