Can you set a method to a variable?

  • A+
Category:Languages

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: