Runnable::new vs new Runnable()












35














Why doesn't the first of the following examples work?





  • run(R::new); method R.run is not called.


  • run(new R()); method R.run is called.


Both examples are compiled-able.



public class ConstructorRefVsNew {

public static void main(String args) {
new ConstructorRefVsNew().run(R::new);
System.out.println("-----------------------");
new ConstructorRefVsNew().run(new R());
}

void run(Runnable r) {
r.run();
}

static class R implements Runnable {

R() {
System.out.println("R constructor runs");
}

@Override
public void run() {
System.out.println("R.run runs");
}
}
}


The output is:



  R constructor runs
-----------------------
R constructor runs
R.run runs


In the first example, the R constructor is called, it returns lambda (which is not object):





But then how is it possible, that the example is compiled successfully?










share|improve this question




















  • 2




    just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
    – Carlos Heuberger
    yesterday








  • 2




    Note that Runnable runnable = R::new; runnable instanceof R -> false
    – Prasad Karunagoda
    yesterday










  • I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
    – kevin
    6 hours ago
















35














Why doesn't the first of the following examples work?





  • run(R::new); method R.run is not called.


  • run(new R()); method R.run is called.


Both examples are compiled-able.



public class ConstructorRefVsNew {

public static void main(String args) {
new ConstructorRefVsNew().run(R::new);
System.out.println("-----------------------");
new ConstructorRefVsNew().run(new R());
}

void run(Runnable r) {
r.run();
}

static class R implements Runnable {

R() {
System.out.println("R constructor runs");
}

@Override
public void run() {
System.out.println("R.run runs");
}
}
}


The output is:



  R constructor runs
-----------------------
R constructor runs
R.run runs


In the first example, the R constructor is called, it returns lambda (which is not object):





But then how is it possible, that the example is compiled successfully?










share|improve this question




















  • 2




    just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
    – Carlos Heuberger
    yesterday








  • 2




    Note that Runnable runnable = R::new; runnable instanceof R -> false
    – Prasad Karunagoda
    yesterday










  • I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
    – kevin
    6 hours ago














35












35








35


3





Why doesn't the first of the following examples work?





  • run(R::new); method R.run is not called.


  • run(new R()); method R.run is called.


Both examples are compiled-able.



public class ConstructorRefVsNew {

public static void main(String args) {
new ConstructorRefVsNew().run(R::new);
System.out.println("-----------------------");
new ConstructorRefVsNew().run(new R());
}

void run(Runnable r) {
r.run();
}

static class R implements Runnable {

R() {
System.out.println("R constructor runs");
}

@Override
public void run() {
System.out.println("R.run runs");
}
}
}


The output is:



  R constructor runs
-----------------------
R constructor runs
R.run runs


In the first example, the R constructor is called, it returns lambda (which is not object):





But then how is it possible, that the example is compiled successfully?










share|improve this question















Why doesn't the first of the following examples work?





  • run(R::new); method R.run is not called.


  • run(new R()); method R.run is called.


Both examples are compiled-able.



public class ConstructorRefVsNew {

public static void main(String args) {
new ConstructorRefVsNew().run(R::new);
System.out.println("-----------------------");
new ConstructorRefVsNew().run(new R());
}

void run(Runnable r) {
r.run();
}

static class R implements Runnable {

R() {
System.out.println("R constructor runs");
}

@Override
public void run() {
System.out.println("R.run runs");
}
}
}


The output is:



  R constructor runs
-----------------------
R constructor runs
R.run runs


In the first example, the R constructor is called, it returns lambda (which is not object):





But then how is it possible, that the example is compiled successfully?







java java-8 runnable constructor-reference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Aomine

41.3k74071




41.3k74071










asked yesterday









user1722245user1722245

727621




727621








  • 2




    just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
    – Carlos Heuberger
    yesterday








  • 2




    Note that Runnable runnable = R::new; runnable instanceof R -> false
    – Prasad Karunagoda
    yesterday










  • I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
    – kevin
    6 hours ago














  • 2




    just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
    – Carlos Heuberger
    yesterday








  • 2




    Note that Runnable runnable = R::new; runnable instanceof R -> false
    – Prasad Karunagoda
    yesterday










  • I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
    – kevin
    6 hours ago








2




2




just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
– Carlos Heuberger
yesterday






just note: a constructor does not return anything - it just constructs the instance. it is more like the new operator that returns the new instance.
– Carlos Heuberger
yesterday






2




2




Note that Runnable runnable = R::new; runnable instanceof R -> false
– Prasad Karunagoda
yesterday




Note that Runnable runnable = R::new; runnable instanceof R -> false
– Prasad Karunagoda
yesterday












I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
– kevin
6 hours ago




I don't know about Java specifically but new is usually an indicator that you want to allocate some memory that you promise you will clean up yourself. R::new just sounds like a factory method, a static function in R that creates and returns an instance of Runnable. If this instance is not captured by assigning it to a variable it might be cleaned up the moment it goes out of scope.
– kevin
6 hours ago












4 Answers
4






active

oldest

votes


















41














Your run method takes a Runnable instance, and that explains why run(new R()) works with the R implementation.



R::new is not equivalent to new R(). It can fit the signature of a Supplier<Runnable> (or similar functional interfaces), but R::new cannot be used as a Runnable implemented with your R class.



A version of your run method that can takeR::new could look like this (but this would be unnecessarily complex):



void run(Supplier<Runnable> r) {
r.get().run();
}



Why does it compile?




Because the compiler can make a Runnable out of the constructor call, and that would be equivalent to this lambda expression version:



new ConstructorRefVsNew().run(() -> {
new R(); //discarded result, but this is the run() body
});


The same applies to these statements:



Runnable runnable = () -> new R();
new ConstructorRefVsNew().run(runnable);
Runnable runnable2 = R::new;
new ConstructorRefVsNew().run(runnable2);


But, as you can notice, the Runnable created with R::new does just call new R() in its run method body.





A valid use of a method reference to execute R#run could use an instance, like this (but you'd surely rather use the r instance directly, in this case):



R r = new R();
new ConstructorRefVsNew().run(r::run);





share|improve this answer























  • Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
    – user1722245
    yesterday






  • 1




    @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
    – ernest_k
    yesterday



















18














The first example:



new ConstructorRefVsNew().run(R::new);


is more or less equivalent to:



new ConstructorRefVsNew().run( () -> {new R();} );


The effect is you just create an instance of R but do not call its run method.






share|improve this answer



















  • 2




    It means, that I have two nested runnable. On the first method run is called only.
    – user1722245
    yesterday



















7














The run method expects a Runnable.



The easy case is new R(). In this case you know the result is an object of type R. R itself is a runnable, it has a run method, and that's how Java sees it.



But when you pass R::new something else is happening. What you tell it is to create an anonymous object compatible with a Runnable whose run method runs the operation you passed it.



The operation you passed it is not R's run method. The operation is the costructor of R. Thus, it's like you have passed it an anonymous class like:



new Runnable() {

public void run() {
new R();
}
}


(Not all the details are the same, but this is the closest "classical" Java construct ).



R::new, when called, calls new R(). Nothing more, nothing less.






share|improve this answer





























    6














    Compare two calls:



    ((Runnable)() -> new R()).run();
    new R().run();


    By ((Runnable)() -> new R()) or ((Runnable) R::new) , you create a new Runnable which does nothing1.



    By new R(), you create an instance of the R class where the run method is well-defined.





    1 Actually, it creates an object of R which has no impact on execution.





    I was thinking of treating 2 invocations identically without modifying the main method. We would need to overload run(Runnable) with run(Supplier<Runnable>).



    class ConstructorRefVsNew {

    public static void main(String args) {
    new ConstructorRefVsNew().run(R::new);
    System.out.println("-----------------------");
    new ConstructorRefVsNew().run(new R());
    }

    void run(Runnable r) {
    r.run();
    }

    void run(Supplier<Runnable> s) {
    run(s.get());
    }

    static class R implements Runnable { ... }
    }





    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%2f54072359%2frunnablenew-vs-new-runnable%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









      41














      Your run method takes a Runnable instance, and that explains why run(new R()) works with the R implementation.



      R::new is not equivalent to new R(). It can fit the signature of a Supplier<Runnable> (or similar functional interfaces), but R::new cannot be used as a Runnable implemented with your R class.



      A version of your run method that can takeR::new could look like this (but this would be unnecessarily complex):



      void run(Supplier<Runnable> r) {
      r.get().run();
      }



      Why does it compile?




      Because the compiler can make a Runnable out of the constructor call, and that would be equivalent to this lambda expression version:



      new ConstructorRefVsNew().run(() -> {
      new R(); //discarded result, but this is the run() body
      });


      The same applies to these statements:



      Runnable runnable = () -> new R();
      new ConstructorRefVsNew().run(runnable);
      Runnable runnable2 = R::new;
      new ConstructorRefVsNew().run(runnable2);


      But, as you can notice, the Runnable created with R::new does just call new R() in its run method body.





      A valid use of a method reference to execute R#run could use an instance, like this (but you'd surely rather use the r instance directly, in this case):



      R r = new R();
      new ConstructorRefVsNew().run(r::run);





      share|improve this answer























      • Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
        – user1722245
        yesterday






      • 1




        @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
        – ernest_k
        yesterday
















      41














      Your run method takes a Runnable instance, and that explains why run(new R()) works with the R implementation.



      R::new is not equivalent to new R(). It can fit the signature of a Supplier<Runnable> (or similar functional interfaces), but R::new cannot be used as a Runnable implemented with your R class.



      A version of your run method that can takeR::new could look like this (but this would be unnecessarily complex):



      void run(Supplier<Runnable> r) {
      r.get().run();
      }



      Why does it compile?




      Because the compiler can make a Runnable out of the constructor call, and that would be equivalent to this lambda expression version:



      new ConstructorRefVsNew().run(() -> {
      new R(); //discarded result, but this is the run() body
      });


      The same applies to these statements:



      Runnable runnable = () -> new R();
      new ConstructorRefVsNew().run(runnable);
      Runnable runnable2 = R::new;
      new ConstructorRefVsNew().run(runnable2);


      But, as you can notice, the Runnable created with R::new does just call new R() in its run method body.





      A valid use of a method reference to execute R#run could use an instance, like this (but you'd surely rather use the r instance directly, in this case):



      R r = new R();
      new ConstructorRefVsNew().run(r::run);





      share|improve this answer























      • Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
        – user1722245
        yesterday






      • 1




        @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
        – ernest_k
        yesterday














      41












      41








      41






      Your run method takes a Runnable instance, and that explains why run(new R()) works with the R implementation.



      R::new is not equivalent to new R(). It can fit the signature of a Supplier<Runnable> (or similar functional interfaces), but R::new cannot be used as a Runnable implemented with your R class.



      A version of your run method that can takeR::new could look like this (but this would be unnecessarily complex):



      void run(Supplier<Runnable> r) {
      r.get().run();
      }



      Why does it compile?




      Because the compiler can make a Runnable out of the constructor call, and that would be equivalent to this lambda expression version:



      new ConstructorRefVsNew().run(() -> {
      new R(); //discarded result, but this is the run() body
      });


      The same applies to these statements:



      Runnable runnable = () -> new R();
      new ConstructorRefVsNew().run(runnable);
      Runnable runnable2 = R::new;
      new ConstructorRefVsNew().run(runnable2);


      But, as you can notice, the Runnable created with R::new does just call new R() in its run method body.





      A valid use of a method reference to execute R#run could use an instance, like this (but you'd surely rather use the r instance directly, in this case):



      R r = new R();
      new ConstructorRefVsNew().run(r::run);





      share|improve this answer














      Your run method takes a Runnable instance, and that explains why run(new R()) works with the R implementation.



      R::new is not equivalent to new R(). It can fit the signature of a Supplier<Runnable> (or similar functional interfaces), but R::new cannot be used as a Runnable implemented with your R class.



      A version of your run method that can takeR::new could look like this (but this would be unnecessarily complex):



      void run(Supplier<Runnable> r) {
      r.get().run();
      }



      Why does it compile?




      Because the compiler can make a Runnable out of the constructor call, and that would be equivalent to this lambda expression version:



      new ConstructorRefVsNew().run(() -> {
      new R(); //discarded result, but this is the run() body
      });


      The same applies to these statements:



      Runnable runnable = () -> new R();
      new ConstructorRefVsNew().run(runnable);
      Runnable runnable2 = R::new;
      new ConstructorRefVsNew().run(runnable2);


      But, as you can notice, the Runnable created with R::new does just call new R() in its run method body.





      A valid use of a method reference to execute R#run could use an instance, like this (but you'd surely rather use the r instance directly, in this case):



      R r = new R();
      new ConstructorRefVsNew().run(r::run);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited yesterday

























      answered yesterday









      ernest_kernest_k

      20.5k42244




      20.5k42244












      • Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
        – user1722245
        yesterday






      • 1




        @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
        – ernest_k
        yesterday


















      • Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
        – user1722245
        yesterday






      • 1




        @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
        – ernest_k
        yesterday
















      Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
      – user1722245
      yesterday




      Should we assume, that java compiler created from my method run(Runnable r) -> run(Supplier<Runnable> r)?
      – user1722245
      yesterday




      1




      1




      @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
      – ernest_k
      yesterday




      @user1722245 I edited the answer. The compiler made () -> {new R();} out of R::new in that context.
      – ernest_k
      yesterday













      18














      The first example:



      new ConstructorRefVsNew().run(R::new);


      is more or less equivalent to:



      new ConstructorRefVsNew().run( () -> {new R();} );


      The effect is you just create an instance of R but do not call its run method.






      share|improve this answer



















      • 2




        It means, that I have two nested runnable. On the first method run is called only.
        – user1722245
        yesterday
















      18














      The first example:



      new ConstructorRefVsNew().run(R::new);


      is more or less equivalent to:



      new ConstructorRefVsNew().run( () -> {new R();} );


      The effect is you just create an instance of R but do not call its run method.






      share|improve this answer



















      • 2




        It means, that I have two nested runnable. On the first method run is called only.
        – user1722245
        yesterday














      18












      18








      18






      The first example:



      new ConstructorRefVsNew().run(R::new);


      is more or less equivalent to:



      new ConstructorRefVsNew().run( () -> {new R();} );


      The effect is you just create an instance of R but do not call its run method.






      share|improve this answer














      The first example:



      new ConstructorRefVsNew().run(R::new);


      is more or less equivalent to:



      new ConstructorRefVsNew().run( () -> {new R();} );


      The effect is you just create an instance of R but do not call its run method.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited yesterday

























      answered yesterday









      HenryHenry

      33.5k54260




      33.5k54260








      • 2




        It means, that I have two nested runnable. On the first method run is called only.
        – user1722245
        yesterday














      • 2




        It means, that I have two nested runnable. On the first method run is called only.
        – user1722245
        yesterday








      2




      2




      It means, that I have two nested runnable. On the first method run is called only.
      – user1722245
      yesterday




      It means, that I have two nested runnable. On the first method run is called only.
      – user1722245
      yesterday











      7














      The run method expects a Runnable.



      The easy case is new R(). In this case you know the result is an object of type R. R itself is a runnable, it has a run method, and that's how Java sees it.



      But when you pass R::new something else is happening. What you tell it is to create an anonymous object compatible with a Runnable whose run method runs the operation you passed it.



      The operation you passed it is not R's run method. The operation is the costructor of R. Thus, it's like you have passed it an anonymous class like:



      new Runnable() {

      public void run() {
      new R();
      }
      }


      (Not all the details are the same, but this is the closest "classical" Java construct ).



      R::new, when called, calls new R(). Nothing more, nothing less.






      share|improve this answer


























        7














        The run method expects a Runnable.



        The easy case is new R(). In this case you know the result is an object of type R. R itself is a runnable, it has a run method, and that's how Java sees it.



        But when you pass R::new something else is happening. What you tell it is to create an anonymous object compatible with a Runnable whose run method runs the operation you passed it.



        The operation you passed it is not R's run method. The operation is the costructor of R. Thus, it's like you have passed it an anonymous class like:



        new Runnable() {

        public void run() {
        new R();
        }
        }


        (Not all the details are the same, but this is the closest "classical" Java construct ).



        R::new, when called, calls new R(). Nothing more, nothing less.






        share|improve this answer
























          7












          7








          7






          The run method expects a Runnable.



          The easy case is new R(). In this case you know the result is an object of type R. R itself is a runnable, it has a run method, and that's how Java sees it.



          But when you pass R::new something else is happening. What you tell it is to create an anonymous object compatible with a Runnable whose run method runs the operation you passed it.



          The operation you passed it is not R's run method. The operation is the costructor of R. Thus, it's like you have passed it an anonymous class like:



          new Runnable() {

          public void run() {
          new R();
          }
          }


          (Not all the details are the same, but this is the closest "classical" Java construct ).



          R::new, when called, calls new R(). Nothing more, nothing less.






          share|improve this answer












          The run method expects a Runnable.



          The easy case is new R(). In this case you know the result is an object of type R. R itself is a runnable, it has a run method, and that's how Java sees it.



          But when you pass R::new something else is happening. What you tell it is to create an anonymous object compatible with a Runnable whose run method runs the operation you passed it.



          The operation you passed it is not R's run method. The operation is the costructor of R. Thus, it's like you have passed it an anonymous class like:



          new Runnable() {

          public void run() {
          new R();
          }
          }


          (Not all the details are the same, but this is the closest "classical" Java construct ).



          R::new, when called, calls new R(). Nothing more, nothing less.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          RealSkepticRealSkeptic

          27.8k63260




          27.8k63260























              6














              Compare two calls:



              ((Runnable)() -> new R()).run();
              new R().run();


              By ((Runnable)() -> new R()) or ((Runnable) R::new) , you create a new Runnable which does nothing1.



              By new R(), you create an instance of the R class where the run method is well-defined.





              1 Actually, it creates an object of R which has no impact on execution.





              I was thinking of treating 2 invocations identically without modifying the main method. We would need to overload run(Runnable) with run(Supplier<Runnable>).



              class ConstructorRefVsNew {

              public static void main(String args) {
              new ConstructorRefVsNew().run(R::new);
              System.out.println("-----------------------");
              new ConstructorRefVsNew().run(new R());
              }

              void run(Runnable r) {
              r.run();
              }

              void run(Supplier<Runnable> s) {
              run(s.get());
              }

              static class R implements Runnable { ... }
              }





              share|improve this answer




























                6














                Compare two calls:



                ((Runnable)() -> new R()).run();
                new R().run();


                By ((Runnable)() -> new R()) or ((Runnable) R::new) , you create a new Runnable which does nothing1.



                By new R(), you create an instance of the R class where the run method is well-defined.





                1 Actually, it creates an object of R which has no impact on execution.





                I was thinking of treating 2 invocations identically without modifying the main method. We would need to overload run(Runnable) with run(Supplier<Runnable>).



                class ConstructorRefVsNew {

                public static void main(String args) {
                new ConstructorRefVsNew().run(R::new);
                System.out.println("-----------------------");
                new ConstructorRefVsNew().run(new R());
                }

                void run(Runnable r) {
                r.run();
                }

                void run(Supplier<Runnable> s) {
                run(s.get());
                }

                static class R implements Runnable { ... }
                }





                share|improve this answer


























                  6












                  6








                  6






                  Compare two calls:



                  ((Runnable)() -> new R()).run();
                  new R().run();


                  By ((Runnable)() -> new R()) or ((Runnable) R::new) , you create a new Runnable which does nothing1.



                  By new R(), you create an instance of the R class where the run method is well-defined.





                  1 Actually, it creates an object of R which has no impact on execution.





                  I was thinking of treating 2 invocations identically without modifying the main method. We would need to overload run(Runnable) with run(Supplier<Runnable>).



                  class ConstructorRefVsNew {

                  public static void main(String args) {
                  new ConstructorRefVsNew().run(R::new);
                  System.out.println("-----------------------");
                  new ConstructorRefVsNew().run(new R());
                  }

                  void run(Runnable r) {
                  r.run();
                  }

                  void run(Supplier<Runnable> s) {
                  run(s.get());
                  }

                  static class R implements Runnable { ... }
                  }





                  share|improve this answer














                  Compare two calls:



                  ((Runnable)() -> new R()).run();
                  new R().run();


                  By ((Runnable)() -> new R()) or ((Runnable) R::new) , you create a new Runnable which does nothing1.



                  By new R(), you create an instance of the R class where the run method is well-defined.





                  1 Actually, it creates an object of R which has no impact on execution.





                  I was thinking of treating 2 invocations identically without modifying the main method. We would need to overload run(Runnable) with run(Supplier<Runnable>).



                  class ConstructorRefVsNew {

                  public static void main(String args) {
                  new ConstructorRefVsNew().run(R::new);
                  System.out.println("-----------------------");
                  new ConstructorRefVsNew().run(new R());
                  }

                  void run(Runnable r) {
                  r.run();
                  }

                  void run(Supplier<Runnable> s) {
                  run(s.get());
                  }

                  static class R implements Runnable { ... }
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  Andrew TobilkoAndrew Tobilko

                  26.7k104284




                  26.7k104284






























                      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%2f54072359%2frunnablenew-vs-new-runnable%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

                      An IMO inspired problem

                      Management

                      Has there ever been an instance of an active nuclear power plant within or near a war zone?