Declaring a method when creating an object












29















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





























    29















    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



























      29












      29








      29


      3






      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
















      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 anonymous-class anonymous-inner-class






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 28 '18 at 18:14









      Andrew Tobilko

      28.3k104588




      28.3k104588










      asked Dec 28 '18 at 15:21









      Sekonoishi KamikiSekonoishi Kamiki

      15213




      15213
























          4 Answers
          4






          active

          oldest

          votes


















          43














          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 Java 10's 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





















          • 9





            Interesting to know that the var keyword changes things here...

            – ernest_k
            Dec 28 '18 at 15:44






          • 1





            Note that var was introduced in Java 10 and will not be available in lower versions.

            – nasch
            Dec 28 '18 at 20:03






          • 5





            @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

            – Sombrero Chicken
            Dec 28 '18 at 20:41






          • 2





            @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

            – ernest_k
            Dec 28 '18 at 20:46






          • 2





            You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

            – jpmc26
            Dec 28 '18 at 23:33





















          21














          In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object. That anonymous class has the additional method a(), which is why you can call it.






          share|improve this answer

































            2














            Java is statically typed. When you say object.a() it is looking for the method a in the Object class which is not present. Hence it does not compile.



            What you can do is get the method of object using reflection as shown below :



            Object object = new Object() {
            public void a() {
            System.out.println("In a");
            }
            }

            Method method = object.getClass().getDeclaredMethod("a");
            method.invoke(object, null);


            This would print




            In a







            share|improve this answer





















            • 2





              That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

              – MatthewRock
              Dec 28 '18 at 19:39






            • 2





              you should have pointed out that calling the method through reflection is the last thing OP wants :)

              – Andrew Tobilko
              Dec 28 '18 at 20:17











            • @MatthewRock The JVM does support dynamic dispatch, however.

              – Solomon Ucko
              Dec 29 '18 at 19:27











            • @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

              – MatthewRock
              Dec 29 '18 at 20:59






            • 1





              @MatthewRock What exactly was wrong?

              – fastcodejava
              Dec 30 '18 at 4:35



















            0














            Don't worry, you will have to do a bit of correction
            Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -



            public class demo {

            public static void main(String args) {
            new Object() {
            public void a() {
            /*code*/
            System.out.println("Hello");
            }
            }.a();

            }

            }


            But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -



            interface Object
            {
            public void a();
            }
            class demo {

            public static void main(String args) {
            Object object = new Object() {
            public void a() {
            System.out.println("Hello");
            }

            }; object.a();


            }

            }


            I hope it will help a bit.






            share|improve this answer



















            • 1





              "to access the private member of a class" what's the private member?

              – Andrew Tobilko
              Dec 30 '18 at 12:46











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









            43














            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 Java 10's 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





















            • 9





              Interesting to know that the var keyword changes things here...

              – ernest_k
              Dec 28 '18 at 15:44






            • 1





              Note that var was introduced in Java 10 and will not be available in lower versions.

              – nasch
              Dec 28 '18 at 20:03






            • 5





              @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

              – Sombrero Chicken
              Dec 28 '18 at 20:41






            • 2





              @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

              – ernest_k
              Dec 28 '18 at 20:46






            • 2





              You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

              – jpmc26
              Dec 28 '18 at 23:33


















            43














            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 Java 10's 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





















            • 9





              Interesting to know that the var keyword changes things here...

              – ernest_k
              Dec 28 '18 at 15:44






            • 1





              Note that var was introduced in Java 10 and will not be available in lower versions.

              – nasch
              Dec 28 '18 at 20:03






            • 5





              @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

              – Sombrero Chicken
              Dec 28 '18 at 20:41






            • 2





              @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

              – ernest_k
              Dec 28 '18 at 20:46






            • 2





              You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

              – jpmc26
              Dec 28 '18 at 23:33
















            43












            43








            43







            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 Java 10's 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 Java 10's 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 Jan 2 at 19:27

























            answered Dec 28 '18 at 15:36









            Andrew TobilkoAndrew Tobilko

            28.3k104588




            28.3k104588








            • 9





              Interesting to know that the var keyword changes things here...

              – ernest_k
              Dec 28 '18 at 15:44






            • 1





              Note that var was introduced in Java 10 and will not be available in lower versions.

              – nasch
              Dec 28 '18 at 20:03






            • 5





              @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

              – Sombrero Chicken
              Dec 28 '18 at 20:41






            • 2





              @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

              – ernest_k
              Dec 28 '18 at 20:46






            • 2





              You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

              – jpmc26
              Dec 28 '18 at 23:33
















            • 9





              Interesting to know that the var keyword changes things here...

              – ernest_k
              Dec 28 '18 at 15:44






            • 1





              Note that var was introduced in Java 10 and will not be available in lower versions.

              – nasch
              Dec 28 '18 at 20:03






            • 5





              @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

              – Sombrero Chicken
              Dec 28 '18 at 20:41






            • 2





              @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

              – ernest_k
              Dec 28 '18 at 20:46






            • 2





              You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

              – jpmc26
              Dec 28 '18 at 23:33










            9




            9





            Interesting to know that the var keyword changes things here...

            – ernest_k
            Dec 28 '18 at 15:44





            Interesting to know that the var keyword changes things here...

            – ernest_k
            Dec 28 '18 at 15:44




            1




            1





            Note that var was introduced in Java 10 and will not be available in lower versions.

            – nasch
            Dec 28 '18 at 20:03





            Note that var was introduced in Java 10 and will not be available in lower versions.

            – nasch
            Dec 28 '18 at 20:03




            5




            5





            @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

            – Sombrero Chicken
            Dec 28 '18 at 20:41





            @ernest_k It's really not that hard or interesting. By saying Object object = you are downcasting to an Object, var gives object its actual correct type that has a() defined.

            – Sombrero Chicken
            Dec 28 '18 at 20:41




            2




            2





            @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

            – ernest_k
            Dec 28 '18 at 20:46





            @SombreroChicken I understand the effect of var here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!

            – ernest_k
            Dec 28 '18 at 20:46




            2




            2





            You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

            – jpmc26
            Dec 28 '18 at 23:33







            You can also declare an interface that has the a() method and create an anonymous object from that instead of Object, which would work in lower versions that don't support var. E.g., private interface HasA { public void a(); } then HasA object = new HasA() { public void a() { ... } };.

            – jpmc26
            Dec 28 '18 at 23:33















            21














            In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object. That anonymous class has the additional method a(), which is why you can call it.






            share|improve this answer






























              21














              In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object. That anonymous class has the additional method a(), which is why you can call it.






              share|improve this answer




























                21












                21








                21







                In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object. That anonymous class has the additional method a(), which is why you can call it.






                share|improve this answer















                In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object. That anonymous class has the additional method a(), which is why you can call it.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 28 '18 at 20:20









                MTCoster

                3,83922141




                3,83922141










                answered Dec 28 '18 at 15:24









                IvanIvan

                5,48111022




                5,48111022























                    2














                    Java is statically typed. When you say object.a() it is looking for the method a in the Object class which is not present. Hence it does not compile.



                    What you can do is get the method of object using reflection as shown below :



                    Object object = new Object() {
                    public void a() {
                    System.out.println("In a");
                    }
                    }

                    Method method = object.getClass().getDeclaredMethod("a");
                    method.invoke(object, null);


                    This would print




                    In a







                    share|improve this answer





















                    • 2





                      That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                      – MatthewRock
                      Dec 28 '18 at 19:39






                    • 2





                      you should have pointed out that calling the method through reflection is the last thing OP wants :)

                      – Andrew Tobilko
                      Dec 28 '18 at 20:17











                    • @MatthewRock The JVM does support dynamic dispatch, however.

                      – Solomon Ucko
                      Dec 29 '18 at 19:27











                    • @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                      – MatthewRock
                      Dec 29 '18 at 20:59






                    • 1





                      @MatthewRock What exactly was wrong?

                      – fastcodejava
                      Dec 30 '18 at 4:35
















                    2














                    Java is statically typed. When you say object.a() it is looking for the method a in the Object class which is not present. Hence it does not compile.



                    What you can do is get the method of object using reflection as shown below :



                    Object object = new Object() {
                    public void a() {
                    System.out.println("In a");
                    }
                    }

                    Method method = object.getClass().getDeclaredMethod("a");
                    method.invoke(object, null);


                    This would print




                    In a







                    share|improve this answer





















                    • 2





                      That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                      – MatthewRock
                      Dec 28 '18 at 19:39






                    • 2





                      you should have pointed out that calling the method through reflection is the last thing OP wants :)

                      – Andrew Tobilko
                      Dec 28 '18 at 20:17











                    • @MatthewRock The JVM does support dynamic dispatch, however.

                      – Solomon Ucko
                      Dec 29 '18 at 19:27











                    • @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                      – MatthewRock
                      Dec 29 '18 at 20:59






                    • 1





                      @MatthewRock What exactly was wrong?

                      – fastcodejava
                      Dec 30 '18 at 4:35














                    2












                    2








                    2







                    Java is statically typed. When you say object.a() it is looking for the method a in the Object class which is not present. Hence it does not compile.



                    What you can do is get the method of object using reflection as shown below :



                    Object object = new Object() {
                    public void a() {
                    System.out.println("In a");
                    }
                    }

                    Method method = object.getClass().getDeclaredMethod("a");
                    method.invoke(object, null);


                    This would print




                    In a







                    share|improve this answer















                    Java is statically typed. When you say object.a() it is looking for the method a in the Object class which is not present. Hence it does not compile.



                    What you can do is get the method of object using reflection as shown below :



                    Object object = new Object() {
                    public void a() {
                    System.out.println("In a");
                    }
                    }

                    Method method = object.getClass().getDeclaredMethod("a");
                    method.invoke(object, null);


                    This would print




                    In a








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 28 '18 at 19:51

























                    answered Dec 28 '18 at 19:31









                    fastcodejavafastcodejava

                    24.5k19109162




                    24.5k19109162








                    • 2





                      That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                      – MatthewRock
                      Dec 28 '18 at 19:39






                    • 2





                      you should have pointed out that calling the method through reflection is the last thing OP wants :)

                      – Andrew Tobilko
                      Dec 28 '18 at 20:17











                    • @MatthewRock The JVM does support dynamic dispatch, however.

                      – Solomon Ucko
                      Dec 29 '18 at 19:27











                    • @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                      – MatthewRock
                      Dec 29 '18 at 20:59






                    • 1





                      @MatthewRock What exactly was wrong?

                      – fastcodejava
                      Dec 30 '18 at 4:35














                    • 2





                      That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                      – MatthewRock
                      Dec 28 '18 at 19:39






                    • 2





                      you should have pointed out that calling the method through reflection is the last thing OP wants :)

                      – Andrew Tobilko
                      Dec 28 '18 at 20:17











                    • @MatthewRock The JVM does support dynamic dispatch, however.

                      – Solomon Ucko
                      Dec 29 '18 at 19:27











                    • @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                      – MatthewRock
                      Dec 29 '18 at 20:59






                    • 1





                      @MatthewRock What exactly was wrong?

                      – fastcodejava
                      Dec 30 '18 at 4:35








                    2




                    2





                    That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                    – MatthewRock
                    Dec 28 '18 at 19:39





                    That's not true. Java uses static typing (you can see it in your example - Object object - you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here

                    – MatthewRock
                    Dec 28 '18 at 19:39




                    2




                    2





                    you should have pointed out that calling the method through reflection is the last thing OP wants :)

                    – Andrew Tobilko
                    Dec 28 '18 at 20:17





                    you should have pointed out that calling the method through reflection is the last thing OP wants :)

                    – Andrew Tobilko
                    Dec 28 '18 at 20:17













                    @MatthewRock The JVM does support dynamic dispatch, however.

                    – Solomon Ucko
                    Dec 29 '18 at 19:27





                    @MatthewRock The JVM does support dynamic dispatch, however.

                    – Solomon Ucko
                    Dec 29 '18 at 19:27













                    @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                    – MatthewRock
                    Dec 29 '18 at 20:59





                    @SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.

                    – MatthewRock
                    Dec 29 '18 at 20:59




                    1




                    1





                    @MatthewRock What exactly was wrong?

                    – fastcodejava
                    Dec 30 '18 at 4:35





                    @MatthewRock What exactly was wrong?

                    – fastcodejava
                    Dec 30 '18 at 4:35











                    0














                    Don't worry, you will have to do a bit of correction
                    Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -



                    public class demo {

                    public static void main(String args) {
                    new Object() {
                    public void a() {
                    /*code*/
                    System.out.println("Hello");
                    }
                    }.a();

                    }

                    }


                    But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -



                    interface Object
                    {
                    public void a();
                    }
                    class demo {

                    public static void main(String args) {
                    Object object = new Object() {
                    public void a() {
                    System.out.println("Hello");
                    }

                    }; object.a();


                    }

                    }


                    I hope it will help a bit.






                    share|improve this answer



















                    • 1





                      "to access the private member of a class" what's the private member?

                      – Andrew Tobilko
                      Dec 30 '18 at 12:46
















                    0














                    Don't worry, you will have to do a bit of correction
                    Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -



                    public class demo {

                    public static void main(String args) {
                    new Object() {
                    public void a() {
                    /*code*/
                    System.out.println("Hello");
                    }
                    }.a();

                    }

                    }


                    But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -



                    interface Object
                    {
                    public void a();
                    }
                    class demo {

                    public static void main(String args) {
                    Object object = new Object() {
                    public void a() {
                    System.out.println("Hello");
                    }

                    }; object.a();


                    }

                    }


                    I hope it will help a bit.






                    share|improve this answer



















                    • 1





                      "to access the private member of a class" what's the private member?

                      – Andrew Tobilko
                      Dec 30 '18 at 12:46














                    0












                    0








                    0







                    Don't worry, you will have to do a bit of correction
                    Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -



                    public class demo {

                    public static void main(String args) {
                    new Object() {
                    public void a() {
                    /*code*/
                    System.out.println("Hello");
                    }
                    }.a();

                    }

                    }


                    But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -



                    interface Object
                    {
                    public void a();
                    }
                    class demo {

                    public static void main(String args) {
                    Object object = new Object() {
                    public void a() {
                    System.out.println("Hello");
                    }

                    }; object.a();


                    }

                    }


                    I hope it will help a bit.






                    share|improve this answer













                    Don't worry, you will have to do a bit of correction
                    Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -



                    public class demo {

                    public static void main(String args) {
                    new Object() {
                    public void a() {
                    /*code*/
                    System.out.println("Hello");
                    }
                    }.a();

                    }

                    }


                    But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -



                    interface Object
                    {
                    public void a();
                    }
                    class demo {

                    public static void main(String args) {
                    Object object = new Object() {
                    public void a() {
                    System.out.println("Hello");
                    }

                    }; object.a();


                    }

                    }


                    I hope it will help a bit.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 30 '18 at 12:28









                    MAYANKMAYANK

                    15




                    15








                    • 1





                      "to access the private member of a class" what's the private member?

                      – Andrew Tobilko
                      Dec 30 '18 at 12:46














                    • 1





                      "to access the private member of a class" what's the private member?

                      – Andrew Tobilko
                      Dec 30 '18 at 12:46








                    1




                    1





                    "to access the private member of a class" what's the private member?

                    – Andrew Tobilko
                    Dec 30 '18 at 12:46





                    "to access the private member of a class" what's the private member?

                    – Andrew Tobilko
                    Dec 30 '18 at 12:46


















                    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.




                    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

                    Wiesbaden

                    Marschland

                    Dieringhausen