Can you set a method to a variable? [closed]












16















Is it possible to do something like this in Java



private ? /* (I dont know what Class to use) */ shortcutToMethod = redundantMethod(game.getGraphics());


So instead of calling redundantMethod(game.getGraphics().doThisMethod());



I could just do shortCutToMethod.doThisMethod();



Is this possible?










share|improve this question















closed as unclear what you're asking by Jon Skeet, idmean, coldspeed, Ilmari Karonen, Moira Jan 22 at 18:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    Maybe you can get a better answer if you show a longer piece of code where you want to have this.

    – Thilo
    Jan 22 at 7:42






  • 3





    Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

    – Stefan Steinegger
    Jan 22 at 7:44






  • 5





    Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

    – Jon Skeet
    Jan 22 at 7:45








  • 5





    @AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

    – Stefan Steinegger
    Jan 22 at 7:52






  • 2





    @AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

    – ghilesZ
    Jan 22 at 11:54


















16















Is it possible to do something like this in Java



private ? /* (I dont know what Class to use) */ shortcutToMethod = redundantMethod(game.getGraphics());


So instead of calling redundantMethod(game.getGraphics().doThisMethod());



I could just do shortCutToMethod.doThisMethod();



Is this possible?










share|improve this question















closed as unclear what you're asking by Jon Skeet, idmean, coldspeed, Ilmari Karonen, Moira Jan 22 at 18:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    Maybe you can get a better answer if you show a longer piece of code where you want to have this.

    – Thilo
    Jan 22 at 7:42






  • 3





    Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

    – Stefan Steinegger
    Jan 22 at 7:44






  • 5





    Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

    – Jon Skeet
    Jan 22 at 7:45








  • 5





    @AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

    – Stefan Steinegger
    Jan 22 at 7:52






  • 2





    @AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

    – ghilesZ
    Jan 22 at 11:54
















16












16








16


2






Is it possible to do something like this in Java



private ? /* (I dont know what Class to use) */ shortcutToMethod = redundantMethod(game.getGraphics());


So instead of calling redundantMethod(game.getGraphics().doThisMethod());



I could just do shortCutToMethod.doThisMethod();



Is this possible?










share|improve this question
















Is it possible to do something like this in Java



private ? /* (I dont know what Class to use) */ shortcutToMethod = redundantMethod(game.getGraphics());


So instead of calling redundantMethod(game.getGraphics().doThisMethod());



I could just do shortCutToMethod.doThisMethod();



Is this possible?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 15:42









Paolo Forgia

4,61262849




4,61262849










asked Jan 22 at 7:40









Erick MooreErick Moore

955




955




closed as unclear what you're asking by Jon Skeet, idmean, coldspeed, Ilmari Karonen, Moira Jan 22 at 18:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Jon Skeet, idmean, coldspeed, Ilmari Karonen, Moira Jan 22 at 18:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2





    Maybe you can get a better answer if you show a longer piece of code where you want to have this.

    – Thilo
    Jan 22 at 7:42






  • 3





    Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

    – Stefan Steinegger
    Jan 22 at 7:44






  • 5





    Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

    – Jon Skeet
    Jan 22 at 7:45








  • 5





    @AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

    – Stefan Steinegger
    Jan 22 at 7:52






  • 2





    @AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

    – ghilesZ
    Jan 22 at 11:54
















  • 2





    Maybe you can get a better answer if you show a longer piece of code where you want to have this.

    – Thilo
    Jan 22 at 7:42






  • 3





    Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

    – Stefan Steinegger
    Jan 22 at 7:44






  • 5





    Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

    – Jon Skeet
    Jan 22 at 7:45








  • 5





    @AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

    – Stefan Steinegger
    Jan 22 at 7:52






  • 2





    @AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

    – ghilesZ
    Jan 22 at 11:54










2




2





Maybe you can get a better answer if you show a longer piece of code where you want to have this.

– Thilo
Jan 22 at 7:42





Maybe you can get a better answer if you show a longer piece of code where you want to have this.

– Thilo
Jan 22 at 7:42




3




3





Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

– Stefan Steinegger
Jan 22 at 7:44





Functional interfaces is what you are looking for. Technically they are regular interfaces, where you assign an instance of a type that implements it. Syntactically, there are some shortcuts that makes it fell like a method or function.

– Stefan Steinegger
Jan 22 at 7:44




5




5





Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

– Jon Skeet
Jan 22 at 7:45







Did you mean redundantMethod(game.getGraphics()).doThisMethod()? It's hard to see how you'd specify that you want to call doThisMethod() on the result of game.getGraphics() and then call redundantMethod passing the result. A complete example would make it much easier to understand what you're trying to achieve.

– Jon Skeet
Jan 22 at 7:45






5




5





@AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

– Stefan Steinegger
Jan 22 at 7:52





@AlexeiKaigorodov: en.wikipedia.org/wiki/Function_pointer

– Stefan Steinegger
Jan 22 at 7:52




2




2





@AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

– ghilesZ
Jan 22 at 11:54







@AlexeiKaigorodov "do you know any language where it is possible?" Any language where function/method/procedures are first class citizens. Scheme, Haskell, Scala, OCaml, Javascript, to name a few.

– ghilesZ
Jan 22 at 11:54














4 Answers
4






active

oldest

votes


















20














In Java, there are various ways. If you take a look at java.util.function package, you can see





  • Function: Takes one argument, produces one result


  • Consumer: Takes one argument, produces nothing.


  • BiConsumer: Takes two arguments, produces nothing.


  • Supplier: Takes no argument, produces one result.


  • Predicate: Boolean value function of one argument


You can used them as inputs for your method and execute it within.






share|improve this answer





















  • 3





    Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

    – Zabuza
    Jan 22 at 7:59



















18














Java 8 has introduced the idea of a Functional Interface, which allows you to essentially assign methods to variables. It includes a number of commonly-used interfaces as well.



Common examples:





  • Consumer<T> - a method that takes in T and returns void


  • Function<T, R> - a method that takes in T and returns R


  • Supplier<R> - a method that takes no arguments and returns R


  • Runnable - a method that takes no arguments and returns void


  • Predicate<T> - a method that takes in T and returns boolean


In your case, you appear to be after a Runnable:



Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();





share|improve this answer


























  • returns void not sure if returning nothing counts as something ;)

    – Lino
    Jan 22 at 7:47






  • 1





    @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

    – Joe C
    Jan 22 at 18:13



















2














You can use functional interfaces. A functional interface allows one to adapt an abstract method to a lambda expression that can in turn be stored in a variable, and that's close to storing a method in a variable.



There are a number of functional interfaces available in Java (you can design others yourself). For example, if your redundantMethod returns nothing, you can use a functional interface appropriate for that:



private Consumer<Graphics> shortcutToMethod = 
graphics -> redundantMethod(game.getGraphics());


It can even go with a method reference:



private Consumer<Graphics> shortcutToMethod = this::redundantMethod; //some rules apply


And that can be called with:



shortcutToMethod.accept(game.getGraphics());


Consumer is one of the functional interfaces that come with Java, and it declares the abstract method accept that is called above. There are others that you can find in the java.util.function package, and you choose or write a particular functional interface based on what signature your particular method has. See java.util.function package for more information.






share|improve this answer































    1














    Well in addition to what the others already wrote. Assuming the returntype is "Graphics" here are 4 examples:



    //You need some kind of forward declaration, name don't matter:
    public static interface FunctionDeclaration{
    public Graphics doThisMethod();
    }

    //here you go and assing your variable
    private FunctionDeclaration shortCutToMethod = game.getGraphics()::doThisMethod;
    //or you want this - not sure?
    // private FunctionDeclaration shortCutToMethod = game::getGraphics;
    // and then you just call it:
    shortCutToMethod.doThisMethod();


    If you want to pass the graphics



    //You need some kind of forward declaration, name don't matter:
    public static interface FunctionDeclaration{
    public Graphics doThisMethod(Graphics g);
    }
    //assign your variable
    private FunctionDeclaration shortCutToMethod = param -> param.doThisMethod();
    //and call it - calls game.getGraphics().doThisMethod()
    shortCutToMethod.doThisMethod(game.getGraphics());


    If your "redundant" method does something:



    //You need some kind of forward declaration, name don't matter:
    public static interface FunctionDeclaration{
    public Graphics doThisMethod(Graphics g);
    }
    //assign your variable
    private FunctionDeclaration shortCutToMethod = param -> redundantMethod(param.doThisMethod());
    //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
    shortCutToMethod.doThisMethod(game.getGraphics());


    If your redundant method does somthing without passing graphics:



    //You need some kind of forward declaration, name don't matter:
    public static interface FunctionDeclaration{
    public Graphics doThisMethod();
    }
    //assign your variable
    private FunctionDeclaration shortCutToMethod = () -> redundantMethod(game.getGraphics().doThisMethod());
    //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
    shortCutToMethod.doThisMethod();


    And so on ...
    Sure enough for the forward declaration you can use any existing interface like the predefined ones Joe and others mentioned(eg. Supplier).






    share|improve this answer
































      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      20














      In Java, there are various ways. If you take a look at java.util.function package, you can see





      • Function: Takes one argument, produces one result


      • Consumer: Takes one argument, produces nothing.


      • BiConsumer: Takes two arguments, produces nothing.


      • Supplier: Takes no argument, produces one result.


      • Predicate: Boolean value function of one argument


      You can used them as inputs for your method and execute it within.






      share|improve this answer





















      • 3





        Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

        – Zabuza
        Jan 22 at 7:59
















      20














      In Java, there are various ways. If you take a look at java.util.function package, you can see





      • Function: Takes one argument, produces one result


      • Consumer: Takes one argument, produces nothing.


      • BiConsumer: Takes two arguments, produces nothing.


      • Supplier: Takes no argument, produces one result.


      • Predicate: Boolean value function of one argument


      You can used them as inputs for your method and execute it within.






      share|improve this answer





















      • 3





        Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

        – Zabuza
        Jan 22 at 7:59














      20












      20








      20







      In Java, there are various ways. If you take a look at java.util.function package, you can see





      • Function: Takes one argument, produces one result


      • Consumer: Takes one argument, produces nothing.


      • BiConsumer: Takes two arguments, produces nothing.


      • Supplier: Takes no argument, produces one result.


      • Predicate: Boolean value function of one argument


      You can used them as inputs for your method and execute it within.






      share|improve this answer















      In Java, there are various ways. If you take a look at java.util.function package, you can see





      • Function: Takes one argument, produces one result


      • Consumer: Takes one argument, produces nothing.


      • BiConsumer: Takes two arguments, produces nothing.


      • Supplier: Takes no argument, produces one result.


      • Predicate: Boolean value function of one argument


      You can used them as inputs for your method and execute it within.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 22 at 7:45









      Lino

      9,36922041




      9,36922041










      answered Jan 22 at 7:42









      mkjhmkjh

      1,1401022




      1,1401022








      • 3





        Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

        – Zabuza
        Jan 22 at 7:59














      • 3





        Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

        – Zabuza
        Jan 22 at 7:59








      3




      3





      Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

      – Zabuza
      Jan 22 at 7:59





      Actually, this will create instances of an Interface instead of linking to the method. It might be worth also mentioning Reflection. Because there you can actually refer to a certain method and apply them to arguments.

      – Zabuza
      Jan 22 at 7:59













      18














      Java 8 has introduced the idea of a Functional Interface, which allows you to essentially assign methods to variables. It includes a number of commonly-used interfaces as well.



      Common examples:





      • Consumer<T> - a method that takes in T and returns void


      • Function<T, R> - a method that takes in T and returns R


      • Supplier<R> - a method that takes no arguments and returns R


      • Runnable - a method that takes no arguments and returns void


      • Predicate<T> - a method that takes in T and returns boolean


      In your case, you appear to be after a Runnable:



      Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
      shortcutToMethod.run();





      share|improve this answer


























      • returns void not sure if returning nothing counts as something ;)

        – Lino
        Jan 22 at 7:47






      • 1





        @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

        – Joe C
        Jan 22 at 18:13
















      18














      Java 8 has introduced the idea of a Functional Interface, which allows you to essentially assign methods to variables. It includes a number of commonly-used interfaces as well.



      Common examples:





      • Consumer<T> - a method that takes in T and returns void


      • Function<T, R> - a method that takes in T and returns R


      • Supplier<R> - a method that takes no arguments and returns R


      • Runnable - a method that takes no arguments and returns void


      • Predicate<T> - a method that takes in T and returns boolean


      In your case, you appear to be after a Runnable:



      Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
      shortcutToMethod.run();





      share|improve this answer


























      • returns void not sure if returning nothing counts as something ;)

        – Lino
        Jan 22 at 7:47






      • 1





        @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

        – Joe C
        Jan 22 at 18:13














      18












      18








      18







      Java 8 has introduced the idea of a Functional Interface, which allows you to essentially assign methods to variables. It includes a number of commonly-used interfaces as well.



      Common examples:





      • Consumer<T> - a method that takes in T and returns void


      • Function<T, R> - a method that takes in T and returns R


      • Supplier<R> - a method that takes no arguments and returns R


      • Runnable - a method that takes no arguments and returns void


      • Predicate<T> - a method that takes in T and returns boolean


      In your case, you appear to be after a Runnable:



      Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
      shortcutToMethod.run();





      share|improve this answer















      Java 8 has introduced the idea of a Functional Interface, which allows you to essentially assign methods to variables. It includes a number of commonly-used interfaces as well.



      Common examples:





      • Consumer<T> - a method that takes in T and returns void


      • Function<T, R> - a method that takes in T and returns R


      • Supplier<R> - a method that takes no arguments and returns R


      • Runnable - a method that takes no arguments and returns void


      • Predicate<T> - a method that takes in T and returns boolean


      In your case, you appear to be after a Runnable:



      Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
      shortcutToMethod.run();






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 22 at 16:11









      Marco13

      42.6k857108




      42.6k857108










      answered Jan 22 at 7:46









      Joe CJoe C

      11.6k62543




      11.6k62543













      • returns void not sure if returning nothing counts as something ;)

        – Lino
        Jan 22 at 7:47






      • 1





        @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

        – Joe C
        Jan 22 at 18:13



















      • returns void not sure if returning nothing counts as something ;)

        – Lino
        Jan 22 at 7:47






      • 1





        @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

        – Joe C
        Jan 22 at 18:13

















      returns void not sure if returning nothing counts as something ;)

      – Lino
      Jan 22 at 7:47





      returns void not sure if returning nothing counts as something ;)

      – Lino
      Jan 22 at 7:47




      1




      1





      @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

      – Joe C
      Jan 22 at 18:13





      @KamilDrakari Yup, that was rather silly of me, thanks for the correction.

      – Joe C
      Jan 22 at 18:13











      2














      You can use functional interfaces. A functional interface allows one to adapt an abstract method to a lambda expression that can in turn be stored in a variable, and that's close to storing a method in a variable.



      There are a number of functional interfaces available in Java (you can design others yourself). For example, if your redundantMethod returns nothing, you can use a functional interface appropriate for that:



      private Consumer<Graphics> shortcutToMethod = 
      graphics -> redundantMethod(game.getGraphics());


      It can even go with a method reference:



      private Consumer<Graphics> shortcutToMethod = this::redundantMethod; //some rules apply


      And that can be called with:



      shortcutToMethod.accept(game.getGraphics());


      Consumer is one of the functional interfaces that come with Java, and it declares the abstract method accept that is called above. There are others that you can find in the java.util.function package, and you choose or write a particular functional interface based on what signature your particular method has. See java.util.function package for more information.






      share|improve this answer




























        2














        You can use functional interfaces. A functional interface allows one to adapt an abstract method to a lambda expression that can in turn be stored in a variable, and that's close to storing a method in a variable.



        There are a number of functional interfaces available in Java (you can design others yourself). For example, if your redundantMethod returns nothing, you can use a functional interface appropriate for that:



        private Consumer<Graphics> shortcutToMethod = 
        graphics -> redundantMethod(game.getGraphics());


        It can even go with a method reference:



        private Consumer<Graphics> shortcutToMethod = this::redundantMethod; //some rules apply


        And that can be called with:



        shortcutToMethod.accept(game.getGraphics());


        Consumer is one of the functional interfaces that come with Java, and it declares the abstract method accept that is called above. There are others that you can find in the java.util.function package, and you choose or write a particular functional interface based on what signature your particular method has. See java.util.function package for more information.






        share|improve this answer


























          2












          2








          2







          You can use functional interfaces. A functional interface allows one to adapt an abstract method to a lambda expression that can in turn be stored in a variable, and that's close to storing a method in a variable.



          There are a number of functional interfaces available in Java (you can design others yourself). For example, if your redundantMethod returns nothing, you can use a functional interface appropriate for that:



          private Consumer<Graphics> shortcutToMethod = 
          graphics -> redundantMethod(game.getGraphics());


          It can even go with a method reference:



          private Consumer<Graphics> shortcutToMethod = this::redundantMethod; //some rules apply


          And that can be called with:



          shortcutToMethod.accept(game.getGraphics());


          Consumer is one of the functional interfaces that come with Java, and it declares the abstract method accept that is called above. There are others that you can find in the java.util.function package, and you choose or write a particular functional interface based on what signature your particular method has. See java.util.function package for more information.






          share|improve this answer













          You can use functional interfaces. A functional interface allows one to adapt an abstract method to a lambda expression that can in turn be stored in a variable, and that's close to storing a method in a variable.



          There are a number of functional interfaces available in Java (you can design others yourself). For example, if your redundantMethod returns nothing, you can use a functional interface appropriate for that:



          private Consumer<Graphics> shortcutToMethod = 
          graphics -> redundantMethod(game.getGraphics());


          It can even go with a method reference:



          private Consumer<Graphics> shortcutToMethod = this::redundantMethod; //some rules apply


          And that can be called with:



          shortcutToMethod.accept(game.getGraphics());


          Consumer is one of the functional interfaces that come with Java, and it declares the abstract method accept that is called above. There are others that you can find in the java.util.function package, and you choose or write a particular functional interface based on what signature your particular method has. See java.util.function package for more information.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 7:56









          ernest_kernest_k

          22.7k42547




          22.7k42547























              1














              Well in addition to what the others already wrote. Assuming the returntype is "Graphics" here are 4 examples:



              //You need some kind of forward declaration, name don't matter:
              public static interface FunctionDeclaration{
              public Graphics doThisMethod();
              }

              //here you go and assing your variable
              private FunctionDeclaration shortCutToMethod = game.getGraphics()::doThisMethod;
              //or you want this - not sure?
              // private FunctionDeclaration shortCutToMethod = game::getGraphics;
              // and then you just call it:
              shortCutToMethod.doThisMethod();


              If you want to pass the graphics



              //You need some kind of forward declaration, name don't matter:
              public static interface FunctionDeclaration{
              public Graphics doThisMethod(Graphics g);
              }
              //assign your variable
              private FunctionDeclaration shortCutToMethod = param -> param.doThisMethod();
              //and call it - calls game.getGraphics().doThisMethod()
              shortCutToMethod.doThisMethod(game.getGraphics());


              If your "redundant" method does something:



              //You need some kind of forward declaration, name don't matter:
              public static interface FunctionDeclaration{
              public Graphics doThisMethod(Graphics g);
              }
              //assign your variable
              private FunctionDeclaration shortCutToMethod = param -> redundantMethod(param.doThisMethod());
              //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
              shortCutToMethod.doThisMethod(game.getGraphics());


              If your redundant method does somthing without passing graphics:



              //You need some kind of forward declaration, name don't matter:
              public static interface FunctionDeclaration{
              public Graphics doThisMethod();
              }
              //assign your variable
              private FunctionDeclaration shortCutToMethod = () -> redundantMethod(game.getGraphics().doThisMethod());
              //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
              shortCutToMethod.doThisMethod();


              And so on ...
              Sure enough for the forward declaration you can use any existing interface like the predefined ones Joe and others mentioned(eg. Supplier).






              share|improve this answer






























                1














                Well in addition to what the others already wrote. Assuming the returntype is "Graphics" here are 4 examples:



                //You need some kind of forward declaration, name don't matter:
                public static interface FunctionDeclaration{
                public Graphics doThisMethod();
                }

                //here you go and assing your variable
                private FunctionDeclaration shortCutToMethod = game.getGraphics()::doThisMethod;
                //or you want this - not sure?
                // private FunctionDeclaration shortCutToMethod = game::getGraphics;
                // and then you just call it:
                shortCutToMethod.doThisMethod();


                If you want to pass the graphics



                //You need some kind of forward declaration, name don't matter:
                public static interface FunctionDeclaration{
                public Graphics doThisMethod(Graphics g);
                }
                //assign your variable
                private FunctionDeclaration shortCutToMethod = param -> param.doThisMethod();
                //and call it - calls game.getGraphics().doThisMethod()
                shortCutToMethod.doThisMethod(game.getGraphics());


                If your "redundant" method does something:



                //You need some kind of forward declaration, name don't matter:
                public static interface FunctionDeclaration{
                public Graphics doThisMethod(Graphics g);
                }
                //assign your variable
                private FunctionDeclaration shortCutToMethod = param -> redundantMethod(param.doThisMethod());
                //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                shortCutToMethod.doThisMethod(game.getGraphics());


                If your redundant method does somthing without passing graphics:



                //You need some kind of forward declaration, name don't matter:
                public static interface FunctionDeclaration{
                public Graphics doThisMethod();
                }
                //assign your variable
                private FunctionDeclaration shortCutToMethod = () -> redundantMethod(game.getGraphics().doThisMethod());
                //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                shortCutToMethod.doThisMethod();


                And so on ...
                Sure enough for the forward declaration you can use any existing interface like the predefined ones Joe and others mentioned(eg. Supplier).






                share|improve this answer




























                  1












                  1








                  1







                  Well in addition to what the others already wrote. Assuming the returntype is "Graphics" here are 4 examples:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod();
                  }

                  //here you go and assing your variable
                  private FunctionDeclaration shortCutToMethod = game.getGraphics()::doThisMethod;
                  //or you want this - not sure?
                  // private FunctionDeclaration shortCutToMethod = game::getGraphics;
                  // and then you just call it:
                  shortCutToMethod.doThisMethod();


                  If you want to pass the graphics



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod(Graphics g);
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = param -> param.doThisMethod();
                  //and call it - calls game.getGraphics().doThisMethod()
                  shortCutToMethod.doThisMethod(game.getGraphics());


                  If your "redundant" method does something:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod(Graphics g);
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = param -> redundantMethod(param.doThisMethod());
                  //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                  shortCutToMethod.doThisMethod(game.getGraphics());


                  If your redundant method does somthing without passing graphics:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod();
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = () -> redundantMethod(game.getGraphics().doThisMethod());
                  //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                  shortCutToMethod.doThisMethod();


                  And so on ...
                  Sure enough for the forward declaration you can use any existing interface like the predefined ones Joe and others mentioned(eg. Supplier).






                  share|improve this answer















                  Well in addition to what the others already wrote. Assuming the returntype is "Graphics" here are 4 examples:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod();
                  }

                  //here you go and assing your variable
                  private FunctionDeclaration shortCutToMethod = game.getGraphics()::doThisMethod;
                  //or you want this - not sure?
                  // private FunctionDeclaration shortCutToMethod = game::getGraphics;
                  // and then you just call it:
                  shortCutToMethod.doThisMethod();


                  If you want to pass the graphics



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod(Graphics g);
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = param -> param.doThisMethod();
                  //and call it - calls game.getGraphics().doThisMethod()
                  shortCutToMethod.doThisMethod(game.getGraphics());


                  If your "redundant" method does something:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod(Graphics g);
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = param -> redundantMethod(param.doThisMethod());
                  //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                  shortCutToMethod.doThisMethod(game.getGraphics());


                  If your redundant method does somthing without passing graphics:



                  //You need some kind of forward declaration, name don't matter:
                  public static interface FunctionDeclaration{
                  public Graphics doThisMethod();
                  }
                  //assign your variable
                  private FunctionDeclaration shortCutToMethod = () -> redundantMethod(game.getGraphics().doThisMethod());
                  //and call it - calls redundantMethod(game.getGraphics().doThisMethod())
                  shortCutToMethod.doThisMethod();


                  And so on ...
                  Sure enough for the forward declaration you can use any existing interface like the predefined ones Joe and others mentioned(eg. Supplier).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 22 at 11:48

























                  answered Jan 22 at 9:07









                  kaikai

                  52629




                  52629















                      Popular posts from this blog

                      Human spaceflight

                      Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                      File:DeusFollowingSea.jpg