0

I want to create a static method that can be called by a variety of different classes. All classes that will call this method have a method called "evaluate", which I want to call from within the method.

All classes and methods involved are static. The "evaluate" method, however, is implemented differently in each class that has it. How do I call the evaluate method from the specific class that called the method each time?

Thanks!!

Here is psudeo code/ more information

The goal of my project is to implement Newton and Bisection approximation methods to an arbitrary number of sig-figs

Concerning the bisection method specifically - it should be able to work with any evaluatable function

This is not an ideal way to do it, but because of the framework of my assignment (high school teacher), each of my different functions are imbedded in a

static class, as a single method called "evaluate".

The bisection method relies on being able to call the evaluate method over and over again to find zeroes. I want to be able to call each specific class's

evaluate from the separate bisection method.

Example of a evaluation class:

//evaluate the function x^2+5x+3
public class Problem1{
 public void main(String[] args){
   //code here

   //call bisectionMethod() here

  }

  //various other methods

  //the one that i'm concerned about
  public static double evaluate(double input){
    //return output for x^2+5x+3
  }

}  //several classes like this, with different functions


public class Bisection{

  //filler methods


  //this one:
  public static double[] bisectionMethod(){  //don't know if it should have inputs - it has to be able to figure out which eval it's using
    //do the bisection method
    call evaluate(double input) here
  }
}
Alex
  • 11
  • 2
  • 2
    Do all the classes implement a common interface? – bradimus Mar 04 '16 at 15:53
  • @bradimus I think that was most of the point of his question; I don't think he's learned interfaces yet, and interfaces are one possible answer to the question (correct me if I'm wrong OP) – Mat Jones Mar 04 '16 at 16:03
  • @mjones.udri i do know interfaces, this is a slightly different problem – Alex Mar 04 '16 at 16:06
  • Generally I would say that you are trying to work against the system and would recommend turning all other static classes and methods into non-static versions. What is your use-case? – Alexander Torstling Mar 04 '16 at 16:06
  • Hi Alex - see the original comment. I tried to make my question more specific. One of my major problems is that this is for a school assignment, so there are some slightly weird / arbitrary restrictions on what I can and cant do – Alex Mar 04 '16 at 16:09

3 Answers3

3

This is a restriction that you cannot put static methods into interface. Workaround is to use Java Reflection API:

public Object callStaticEvaluate(Class<?> clazz, double input) throws Exception {
    return clazz.getMethod("evaluate", double.class).invoke(null, input);
}

What is reflection and why is it useful?

Community
  • 1
  • 1
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
2

Given Java 8 syntax there is a third way besides using interfaces and reflection:

In Bisection write

import java.util.function.DoubleUnaryOperator;

public class Bisection {

  public static double[] bisectionMethod(DoubleUnaryOperator evalFn, <other args>)
    // invoke the function
    double in  = ...;
    double out = fn.applyAsDouble(in);
    ...
  }
}   

and invoke Bisection using a method handle to your static evaluation function:

Bisection.bisectionMethod(Problem1::evaluate)
wero
  • 32,544
  • 3
  • 59
  • 84
  • such interface already exists in JDK: `DoubleUnaryOperator` (https://docs.oracle.com/javase/8/docs/api/java/util/function/DoubleUnaryOperator.html) – Alex Salauyou Mar 04 '16 at 16:20
  • In addition, there are several problems with the code above, including a method declaration, an undefined local variable, and a method call that doesn't match the declaration. – Andy Thomas Mar 04 '16 at 16:20
  • @SashaSalauyou great - looked but didn't found this interface - incorporated it in my answer – wero Mar 04 '16 at 16:24
1

You cannot do it via any clean way.. OOP does not support any of such structure. You have to use reflection. Invoking a static method using reflection

so it would be -

public static <T extends XXX> void evaluate(Class<T> c){
        // invoke static method on c using reflection
    }
Community
  • 1
  • 1
Anand Vaidya
  • 1,374
  • 11
  • 26