Declaring a method when creating an object












7














Why first way is correct, but second isn't?





First way:



new Object() {
public void a() {
/*code*/
}
}.a();




Second way:



Object object = new Object() {
public void a() {
/*code*/
}
};

object.a();




And where can I find more information about it?










share|improve this question









New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    a is only in scope locally. When you try to access it in the second block, it is out of scope
    – GBlodgett
    2 hours ago








  • 2




    Because a() is not a method of the Object class
    – Federico Peralta Schaffner
    2 hours ago
















7














Why first way is correct, but second isn't?





First way:



new Object() {
public void a() {
/*code*/
}
}.a();




Second way:



Object object = new Object() {
public void a() {
/*code*/
}
};

object.a();




And where can I find more information about it?










share|improve this question









New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    a is only in scope locally. When you try to access it in the second block, it is out of scope
    – GBlodgett
    2 hours ago








  • 2




    Because a() is not a method of the Object class
    – Federico Peralta Schaffner
    2 hours ago














7












7








7







Why first way is correct, but second isn't?





First way:



new Object() {
public void a() {
/*code*/
}
}.a();




Second way:



Object object = new Object() {
public void a() {
/*code*/
}
};

object.a();




And where can I find more information about it?










share|improve this question









New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Why first way is correct, but second isn't?





First way:



new Object() {
public void a() {
/*code*/
}
}.a();




Second way:



Object object = new Object() {
public void a() {
/*code*/
}
};

object.a();




And where can I find more information about it?







java object methods






share|improve this question









New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









ernest_k

19.7k41942




19.7k41942






New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Sekonoishi Kamiki

361




361




New contributor




Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sekonoishi Kamiki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    a is only in scope locally. When you try to access it in the second block, it is out of scope
    – GBlodgett
    2 hours ago








  • 2




    Because a() is not a method of the Object class
    – Federico Peralta Schaffner
    2 hours ago














  • 2




    a is only in scope locally. When you try to access it in the second block, it is out of scope
    – GBlodgett
    2 hours ago








  • 2




    Because a() is not a method of the Object class
    – Federico Peralta Schaffner
    2 hours ago








2




2




a is only in scope locally. When you try to access it in the second block, it is out of scope
– GBlodgett
2 hours ago






a is only in scope locally. When you try to access it in the second block, it is out of scope
– GBlodgett
2 hours ago






2




2




Because a() is not a method of the Object class
– Federico Peralta Schaffner
2 hours ago




Because a() is not a method of the Object class
– Federico Peralta Schaffner
2 hours ago












2 Answers
2






active

oldest

votes


















12














In second option you assign your new object to a reference of type Object because of this only methods defined in java.lang.Object could be called on that reference.



And in first option you basically create new object of anonymous class that extends java.lang.Object. And that anonymous class has additional method a() that is why you can call it.






share|improve this answer





























    11














    java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1).



    Use local variable type inference (var) to make the second option as valid as the first one.



    var object = new Object() {
    public void a() {}
    };
    object.a();





    share|improve this answer



















    • 6




      Interesting to know that the var keyword changes things here...
      – ernest_k
      1 hour ago











    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
    });


    }
    });






    Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53960664%2fdeclaring-a-method-when-creating-an-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    In second option you assign your new object to a reference of type Object because of this only methods defined in java.lang.Object could be called on that reference.



    And in first option you basically create new object of anonymous class that extends java.lang.Object. And that anonymous class has additional method a() that is why you can call it.






    share|improve this answer


























      12














      In second option you assign your new object to a reference of type Object because of this only methods defined in java.lang.Object could be called on that reference.



      And in first option you basically create new object of anonymous class that extends java.lang.Object. And that anonymous class has additional method a() that is why you can call it.






      share|improve this answer
























        12












        12








        12






        In second option you assign your new object to a reference of type Object because of this only methods defined in java.lang.Object could be called on that reference.



        And in first option you basically create new object of anonymous class that extends java.lang.Object. And that anonymous class has additional method a() that is why you can call it.






        share|improve this answer












        In second option you assign your new object to a reference of type Object because of this only methods defined in java.lang.Object could be called on that reference.



        And in first option you basically create new object of anonymous class that extends java.lang.Object. And that anonymous class has additional method a() that is why you can call it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        Ivan

        4,7641721




        4,7641721

























            11














            java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1).



            Use local variable type inference (var) to make the second option as valid as the first one.



            var object = new Object() {
            public void a() {}
            };
            object.a();





            share|improve this answer



















            • 6




              Interesting to know that the var keyword changes things here...
              – ernest_k
              1 hour ago
















            11














            java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1).



            Use local variable type inference (var) to make the second option as valid as the first one.



            var object = new Object() {
            public void a() {}
            };
            object.a();





            share|improve this answer



















            • 6




              Interesting to know that the var keyword changes things here...
              – ernest_k
              1 hour ago














            11












            11








            11






            java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1).



            Use local variable type inference (var) to make the second option as valid as the first one.



            var object = new Object() {
            public void a() {}
            };
            object.a();





            share|improve this answer














            java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1).



            Use local variable type inference (var) to make the second option as valid as the first one.



            var object = new Object() {
            public void a() {}
            };
            object.a();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            Andrew Tobilko

            25.6k104184




            25.6k104184








            • 6




              Interesting to know that the var keyword changes things here...
              – ernest_k
              1 hour ago














            • 6




              Interesting to know that the var keyword changes things here...
              – ernest_k
              1 hour ago








            6




            6




            Interesting to know that the var keyword changes things here...
            – ernest_k
            1 hour ago




            Interesting to know that the var keyword changes things here...
            – ernest_k
            1 hour ago










            Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.













            Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.












            Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f53960664%2fdeclaring-a-method-when-creating-an-object%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

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio