Can you set a method to a variable? [closed]
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
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.
|
show 7 more comments
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
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 meanredundantMethod(game.getGraphics()).doThisMethod()
? It's hard to see how you'd specify that you want to calldoThisMethod()
on the result ofgame.getGraphics()
and then callredundantMethod
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
|
show 7 more comments
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
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
java
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 meanredundantMethod(game.getGraphics()).doThisMethod()
? It's hard to see how you'd specify that you want to calldoThisMethod()
on the result ofgame.getGraphics()
and then callredundantMethod
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
|
show 7 more comments
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 meanredundantMethod(game.getGraphics()).doThisMethod()
? It's hard to see how you'd specify that you want to calldoThisMethod()
on the result ofgame.getGraphics()
and then callredundantMethod
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
|
show 7 more comments
4 Answers
4
active
oldest
votes
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.
3
Actually, this will create instances of anInterface
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
add a comment |
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 inT
and returnsvoid
Function<T, R>
- a method that takes inT
and returnsR
Supplier<R>
- a method that takes no arguments and returnsR
Runnable
- a method that takes no arguments and returnsvoid
Predicate<T>
- a method that takes inT
and returnsboolean
In your case, you appear to be after a Runnable
:
Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();
returnsvoid
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
add a comment |
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.
add a comment |
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).
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
3
Actually, this will create instances of anInterface
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
add a comment |
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.
3
Actually, this will create instances of anInterface
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
add a comment |
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.
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.
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 anInterface
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
add a comment |
3
Actually, this will create instances of anInterface
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
add a comment |
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 inT
and returnsvoid
Function<T, R>
- a method that takes inT
and returnsR
Supplier<R>
- a method that takes no arguments and returnsR
Runnable
- a method that takes no arguments and returnsvoid
Predicate<T>
- a method that takes inT
and returnsboolean
In your case, you appear to be after a Runnable
:
Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();
returnsvoid
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
add a comment |
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 inT
and returnsvoid
Function<T, R>
- a method that takes inT
and returnsR
Supplier<R>
- a method that takes no arguments and returnsR
Runnable
- a method that takes no arguments and returnsvoid
Predicate<T>
- a method that takes inT
and returnsboolean
In your case, you appear to be after a Runnable
:
Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();
returnsvoid
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
add a comment |
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 inT
and returnsvoid
Function<T, R>
- a method that takes inT
and returnsR
Supplier<R>
- a method that takes no arguments and returnsR
Runnable
- a method that takes no arguments and returnsvoid
Predicate<T>
- a method that takes inT
and returnsboolean
In your case, you appear to be after a Runnable
:
Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();
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 inT
and returnsvoid
Function<T, R>
- a method that takes inT
and returnsR
Supplier<R>
- a method that takes no arguments and returnsR
Runnable
- a method that takes no arguments and returnsvoid
Predicate<T>
- a method that takes inT
and returnsboolean
In your case, you appear to be after a Runnable
:
Runnable shortcutToMethod = () -> redundantMethod(game.getGraphics());
shortcutToMethod.run();
edited Jan 22 at 16:11
Marco13
42.6k857108
42.6k857108
answered Jan 22 at 7:46
Joe CJoe C
11.6k62543
11.6k62543
returnsvoid
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
add a comment |
returnsvoid
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 22 at 7:56
ernest_kernest_k
22.7k42547
22.7k42547
add a comment |
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Jan 22 at 11:48
answered Jan 22 at 9:07
kaikai
52629
52629
add a comment |
add a comment |
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 calldoThisMethod()
on the result ofgame.getGraphics()
and then callredundantMethod
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