Possible Duplicate:
Java Pass Method as Parameter
Is it possible to pass a method as a parameter in Java? If I can't what would be the best course of action for the method below without having to repeat code.
public void enterUserInput(javax.swing.JTextField inputField, javax.swing.JTextField outputField, method rangeChecking){
String input;
double output;
input = inputField.getText();
output = Double.parseDouble(input);
if(rangeChecking(output)){
outputField.setText(input);
inputField.setText(null);
}
I will be calling a rangeChecking method from a different class and each time I call the enterUserInput the rangeChecking method will be different
There's no concept of a passing method as a parameter in Java from scratch. However, we can achieve this by using the lambda function and method reference in Java 8.
We can't directly pass the whole method as an argument to another method. Instead, we can call the method from the argument of another method. // pass method2 as argument to method1 public void method1(method2()); Here, the returned value from method2() is assigned as an argument to method1() .
It's not the interface "object" being passed to the method, still just a regular object. It's just a way of saying "this parameter will accept any object that supports this interface". It's equivalent to accepting some object of a base class type, even if you're passing in a subclass.
Java always passes arguments by value, NOT by reference.
You should use an interface to do it.
You pass the interface that declares the desired function to the method, and invoke the desired method. The specific method that will be invoked is the one implemented by the implementing class.
It is called the strategy design pattern.
code sample:
public static interface Foo { //this is the interface declaration
public void print();
}
public static class Bar implements Foo { //Bar is an implementing class
public void print() {
System.out.println("Bar");
}
}
public static void test(Foo foo) { //the method accepts an interface
foo.print(); //and invokes it, the implementing class' print() will be invoked.
}
public static void main(String... args) throws Exception {
test(new Bar()); //invoke test() with the class implementing the interface
}
Create an interface with that method signature, and pass an anonymous subclass of it with your method implementation.
// the interface that contains the method/methods you want to pass
interface MethodWrapper {
public void method(String s); }
// ...
// in your code
// assuming that "observable" is an object with a method "setCallback" that
// accepts the callback
// note that your method implementation will have access to your local variables
public void someMethodOfYours() {
observable.setCallback(new MethodWrapper() {
public void method(String s) {
System.out.println(s);
} }
);
// note that your anon subclass will have access to all your contain method
// locals and your containing class members, with [some restrictions][1]
}
// in the "observable" class - the class that receives the "callback method"
// ...
if( updated() ) {
callback.method("updated!"); }
In future versions of java (with the advent of lambdas) you won't have to define the interface and the anonymous inner class - there will be specific lambda syntax which will compile to the equivalent bytecode.
[1] Cannot refer to a non-final variable inside an inner class defined in a different method
Not as far as I know. This is generally done by making an interface that has the method you want and passing a class that implements that interface in.
public interface IRangeCheck
{
boolean check(double val);
}
public class RangeChecker implements IRangeCheck
{
public boolean check(double val)
{
return val > 0.0;
}
}
...
public void blabla(..., IRangeCheck irc)
{
...
if(irc.check(output))
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With