can you set a method to a variable in java?












6















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




















  • 2





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

    – Thilo
    3 hours ago











  • do you know any language where it is possible?

    – Alexei Kaigorodov
    3 hours ago






  • 1





    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
    3 hours ago






  • 2





    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
    3 hours ago








  • 1





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

    – Stefan Steinegger
    3 hours ago
















6















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




















  • 2





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

    – Thilo
    3 hours ago











  • do you know any language where it is possible?

    – Alexei Kaigorodov
    3 hours ago






  • 1





    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
    3 hours ago






  • 2





    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
    3 hours ago








  • 1





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

    – Stefan Steinegger
    3 hours ago














6












6








6








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 3 hours ago









deHaar

2,39431528




2,39431528










asked 3 hours ago









Erick MooreErick Moore

393




393








  • 2





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

    – Thilo
    3 hours ago











  • do you know any language where it is possible?

    – Alexei Kaigorodov
    3 hours ago






  • 1





    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
    3 hours ago






  • 2





    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
    3 hours ago








  • 1





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

    – Stefan Steinegger
    3 hours ago














  • 2





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

    – Thilo
    3 hours ago











  • do you know any language where it is possible?

    – Alexei Kaigorodov
    3 hours ago






  • 1





    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
    3 hours ago






  • 2





    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
    3 hours ago








  • 1





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

    – Stefan Steinegger
    3 hours ago








2




2





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

– Thilo
3 hours ago





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

– Thilo
3 hours ago













do you know any language where it is possible?

– Alexei Kaigorodov
3 hours ago





do you know any language where it is possible?

– Alexei Kaigorodov
3 hours ago




1




1





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
3 hours ago





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
3 hours ago




2




2





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
3 hours ago







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
3 hours ago






1




1





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

– Stefan Steinegger
3 hours ago





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

– Stefan Steinegger
3 hours ago












4 Answers
4






active

oldest

votes


















8














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


























  • 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
    3 hours ago



















3














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<T> - 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
    3 hours ago



















0














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































    0














    Well in addition to what the others already wrote. Assuming the returntype is "Graphics".



    //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();


    and 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());


    And 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());


    Or same principle:



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










    New contributor




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




















      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%2f54303371%2fcan-you-set-a-method-to-a-variable-in-java%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









      8














      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


























      • 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
        3 hours ago
















      8














      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


























      • 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
        3 hours ago














      8












      8








      8







      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 3 hours ago









      Lino

      7,90921936




      7,90921936










      answered 3 hours ago









      mkjhmkjh

      1,015919




      1,015919













      • 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
        3 hours ago



















      • 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
        3 hours ago

















      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
      3 hours ago





      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
      3 hours ago













      3














      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<T> - 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
        3 hours ago
















      3














      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<T> - 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
        3 hours ago














      3












      3








      3







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










      answered 3 hours ago









      Joe CJoe C

      11.1k62441




      11.1k62441













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

        – Lino
        3 hours ago



















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

        – Lino
        3 hours ago

















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

      – Lino
      3 hours ago





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

      – Lino
      3 hours ago











      0














      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




























        0














        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


























          0












          0








          0







          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 3 hours ago









          ernest_kernest_k

          21k42344




          21k42344























              0














              Well in addition to what the others already wrote. Assuming the returntype is "Graphics".



              //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();


              and 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());


              And 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());


              Or same principle:



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










              New contributor




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

























                0














                Well in addition to what the others already wrote. Assuming the returntype is "Graphics".



                //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();


                and 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());


                And 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());


                Or same principle:



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










                New contributor




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























                  0












                  0








                  0







                  Well in addition to what the others already wrote. Assuming the returntype is "Graphics".



                  //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();


                  and 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());


                  And 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());


                  Or same principle:



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










                  New contributor




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










                  Well in addition to what the others already wrote. Assuming the returntype is "Graphics".



                  //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();


                  and 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());


                  And 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());


                  Or same principle:



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










                  New contributor




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









                  share|improve this answer



                  share|improve this answer








                  edited 3 mins ago





















                  New contributor




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









                  answered 2 hours ago









                  kaikai

                  143




                  143




                  New contributor




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





                  New contributor





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






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






























                      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%2f54303371%2fcan-you-set-a-method-to-a-variable-in-java%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

                      Callistus I

                      Tabula Rosettana

                      How to label and detect the document text images