Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Array of static methods

Tags:

java

arrays

I would like to create an array containing static methods (or containing references to static methods). I have tried to create an array of classes which implement an interface with the method. With this method, I would get the object and then call the method on it. This does not work for static methods. Is there a way of doing it in Java?

EDIT: Here is the method I have used so far:

interface TableElement{
    public Integer lookup(int value);
}

TableElement[] table = new TableElement[]
{
    new TableElement() { public Integer lookup(int value) { return 0; } },
    new TableElement() { public Integer lookup(int value) { return value * 3; } },
    new TableElement() { public Integer lookup(int value) { return value * value + 3; } },
};

public Integer find(int i, int value) {
    return table[i].lookup(value);
}

I would like the find method to be static.

like image 399
superbriggs Avatar asked Nov 28 '25 05:11

superbriggs


1 Answers

Of course, you can have an array of Method and then you can call it using invoke, check these examples: How do I invoke a private static method using reflection (Java)?

like image 137
Hola Soy Edu Feliz Navidad Avatar answered Nov 30 '25 18:11

Hola Soy Edu Feliz Navidad