0

I dont know if its posible but can I store methods or functions in an array? I know Multi dimensional array now and use it to store many arrays as i want. What i would like to do now is to store the methods or functions I create in a certain class. Because i want to store all of my functions to a certain class then call it if i want using loop. And to make my coding cleaner and easy to understand. Example:

public String[] getDesiredFunction = {getName(),getLastname(),getMiddle()};
for(int i = 0;i<3;i++){
    if(i == 1){
        getDesiredFunction[i];
    }
}

like that? Is it posible?

hsz
  • 148,279
  • 62
  • 259
  • 315
thenewbie
  • 755
  • 19
  • 40
  • You may implement it using switch statement. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – iamtheexp01 Jul 13 '12 at 07:15

5 Answers5

4

You can't do exactly what you want, but can do something similar using an interface:

interface Function {
    public void run();
}

class GetNameFunction implements Function {
    public void run() {
        //do stuff
    }
}
...

And then you can write like this:

Function[] functions = {new GetNameFunction()};
    for(int i = 0; i < functions.length; i++){
        functions[i].run();
    }
}
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • interface? ahm are you talking about the view which for example i will store it in class? hehe – thenewbie Jul 13 '12 at 08:00
  • @thenewbie: I don't understand what you mean here. Using an `interface`, you basically surround your functions with a holder object, which allows you to create an array of those holder objects, as I showed you above. – Keppil Jul 13 '12 at 08:04
  • Ok i get it.. Im not that good in java but i will definitely study this interface method if thats the solution to my problem. Thanks anyway. – thenewbie Jul 13 '12 at 08:09
  • @thenewbie: I would recommend reading up on this and related topics in the [Java Tutorials](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html). – Keppil Jul 13 '12 at 08:12
  • 2
    You basically redefined the Runnable interface here – OneCricketeer Feb 23 '18 at 04:31
  • In Java 8 or later the interface can be implemented using a lambda expression or a method reference. – Johannes Kuhn Jul 16 '22 at 16:15
1

Java 6 does not have first-order functions, but you can use a functional object pattern:

interface NullaryFunction< B > {
    B f();
}

public class Example {
    private final Map< String, NullaryFunction< String > > mFuncs = new HashMap< String, NullaryFunction< String > >() { {
        put( "getName", fncGetName );
        put( "getLastname", fncGetLastname );
        put( "getMiddle", fncGetMiddle );
    } };
    public String getName() { /* ... */ }
    private NullaryFunction< String > fncGetName = new NullaryFunction< String >() {
        @Override String f() { return getName(); }
    };
    public String getMiddle() { /* ... */ }
    private NullaryFunction< String > fncGetMiddle = new NullaryFunction< String >() {
        @Override String f() { return getMiddle(); }
    };
    public String getLastname() { /* ... */ }
    private NullaryFunction< String > fncGetLastname = new NullaryFunction< String >() {
        @Override String f() { return getLastname(); }
    };

    public String runAFunction( String strName ) {
        return mFuncs.get(strName).f();
    }
}
Judge Mental
  • 5,209
  • 17
  • 22
0

Java is not well suited for your use case. It is possible do with reflection.

As a starting point you could look into the reflection trail or see the related stackoverflow question.

Community
  • 1
  • 1
dvberkel
  • 663
  • 4
  • 16
0

You can store object instance and method name as string, using reflection can invoke this method like this:

public class Test {


public void sayHello(){
    System.out.println("hello");
}
/**
 * @param args
 */
public static void main(String[] args) {
    Test test = new Test();
    String methodName = "sayHello";
    try {
        Method m = test.getClass().getMethod(methodName, null);
        m.invoke(test, null);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

}

You need refer java refection

Jason
  • 1,241
  • 8
  • 16
  • you can define the class which have two fields : one is object reference and another is method name, so you can store this class instance in an array. – Jason Jul 13 '12 at 07:30
  • Basically because reflection breaks java language idea (safe and simple) and has bad performance. More discussions here http://advint.extrapro.ru/articles/reflection.php – mishadoff Jul 13 '12 at 11:33
-2

The way you stated in the question won't work. As a alternative you could do it like:

public String[] getDesiredFunction = {getName(),getLastname(),getMiddle()};
for(int i = 0;i<3;i++){
if(getDesiredFunction[i].equals("getName()")){
     getName();
  }
}
consprice
  • 722
  • 3
  • 11
  • 21