Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass method as a parameter in Java? [duplicate]

Tags:

java

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

like image 846
Indy411 Avatar asked Sep 27 '12 08:09

Indy411


People also ask

Can you pass a method as a parameter in Java?

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.

How do you pass a method as a parameter in another method in Java?

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() .

How do you pass an interface object as a parameter in Java?

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.

Does Java pass by reference or copy?

Java always passes arguments by value, NOT by reference.


3 Answers

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
}
like image 146
amit Avatar answered Oct 21 '22 07:10

amit


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

like image 35
jrharshath Avatar answered Oct 21 '22 06:10

jrharshath


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))
           ...
    }
like image 6
CrazyCasta Avatar answered Oct 21 '22 06:10

CrazyCasta