3

I want to make a method that does a specific task, but then calls another method with the results of that task. Well easy enough, but the trick is that I want the method (the second one thats getting called by the first one) to be a parameter of the first method.

I probably explained that terribly, so here is what I think it might look like

public static void main(String[] args) {
    double[][] data = {
        {2,6},
        {-32,5}
    }

    loop(data,{System.out.println(row * col)});
}

public static void loop(double[][] data,somemethod(row,col)) {
    for (int row = 0;row < data.length;row++)
        for (int col = 0;col < data[0].length;col++)
            somemethod(row,col);
}

So the loop method does a task, and then runs the code that was passed as a parameter. Can this be done in java? I feel like I have seen it somewhere.

5
  • Yes In Java8 Lambda Expression Commented Mar 11, 2015 at 20:05
  • Is there no way whatsoever to do this for java 7? Java 7 is ideal for me Commented Mar 11, 2015 at 20:07
  • You should consider Google's Guava Commented Mar 11, 2015 at 20:10
  • There is no callback in java. In java 7 you can do something "similar" with interface (or abstract class) . Commented Mar 11, 2015 at 20:10
  • @Duffydake , Please read What are callback in java Commented Mar 11, 2015 at 20:12

3 Answers 3

5

The pre-Java-8 way to do this was by creating an interface with the method you want called:

interface MyCallback {
    void someMethod(int row, int col);
}

You create an object with the implementation you want:

class MyCallbackImpl implements MyCallback {

    public void somemethod(int row, int col) {
        System.out.println(row * col);
    }
}

and have the method take a parameter implementing that interface:

public static void loop(double[][] data, MyCallback mycallback) {
    for (int row = 0;row < data.length;row++)
        for (int col = 0;col < data[0].length;col++)
            mycallback.somemethod(row,col);
}

If you'd rather not create a separate named class for this you have the alternative of creating an object using an anonymous inner class, calling your loop method like:

loop(data, new MyCallback() {
    public void somemethod(int row, int col) {
        System.out.println(row * col);
    }});
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you can include a lambda example as well?
0

In Java 8 the Lambda expressions make that possible. It was not possible before. This is what I know.

4 Comments

Better to Post your answer with a Working Example
Yes, an example would be much appreciated!
It was technically possible via custom interface
Ok will add one soon.
0

You can use Reflection to do this trick!

First, you find the method like this:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}

In your case, you will send methodName and the parameters of methodName as parameters of loop class. After finding the method, you execute it as:

try {
  method.invoke(param1, param1,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {

Hope it helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.