3

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.

Dude Bro
  • 1,152
  • 1
  • 10
  • 13
superbriggs
  • 629
  • 1
  • 11
  • 19
  • Can you supply the code that you've written? It would help in seeing what you've tried so far. – Krease Feb 01 '13 at 18:57
  • 2
    Why would you want to do this? – Samuel Edwin Ward Feb 01 '13 at 19:34
  • I am writing a program which is given a map and saves a .java file which is a perfect static hashtable, which can be compiled into other programs. It would silly if this was not static in the .java file. – superbriggs Feb 01 '13 at 20:12
  • If you're generating Java code, it would seem you could just call the static methods directly in that code. – Samuel Edwin Ward Feb 01 '13 at 20:34
  • The methods are mapped to numbers generated by the hash function. How could I efficiently map them? – superbriggs Feb 01 '13 at 21:08
  • Because it is perfect hashing there are two layers of hashtables. The final code will have methods which either return null, check to see if it is the only element in its subtable, or act as a sub hashtable. I want to store the data for these in a way that is static. – superbriggs Feb 01 '13 at 22:14

2 Answers2

3

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)?

Community
  • 1
  • 1
0

If you can meet the following conditions:

  1. You know all of the keys at code generation time.
  2. You know all of the values (methods) at code generation time.

You can use code like this:

public class Table {
    public static int hash(String key) {
        /* you can use any type of key and whatever hash function is
         * appropriate; this just meant as a simple example.
         */
        return key.length();
    }

    public static Integer find(String s, int value) {
        int h = hash(s);

        switch (h) {
          case 4: // "zero"
            if (s.equals("zero"))
                return methodZero(value);

          case 6: // "triple"
            if (s.equals("triple"))
                return methodTriple(value);

          case 11: // "squarePlus3"
            if (s.equals("squarePlus3"))
                return methodSquarePlus3(value);

          default:
            throw new UnsupportedOperationException(s);
        }
    }

    private static Integer methodZero(int value) { return 0; };
    private static Integer methodTriple(int value) { return value * 3; };
    private static Integer methodSquarePlus3(int value) { return value * value + 3; };

    /**
     * Just a demo.
     */
    public static void main(String arguments[]) {
        System.out.println("Zero(5): " + find("zero", 5));
        System.out.println("Triple(5): " + find("triple", 5));
        System.out.println("SquarePlus3(5): " + find("squarePlus3", 5));
        System.out.println("boom!");
        find("missingcode", 5);
    }
}

If you need to relax either of the requirements I don't believe you can do everything statically.

If you want to be able to add new keys, you'll have to create a normal hash table at adding time to store them. (You can check it in the default code.)

If you want to be able to replace values, you'll have to use a level of indirection there, probably using Method objects or implementations of Callable (You can call these from within the body of the methodZero methods).

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
  • I do not want to change the data, it will generate the .java file and then I want that static method to return the value as fast as possible. I have considered the switch technique, but I have assumed that it would evaluate the first case, then second, etc. If I was to work with very large data set, then this could take a long time. The idea behind the array was that I could access the methods as fast as possible. Does the switch evaluate every case one-after-another? – superbriggs Feb 01 '13 at 23:52
  • I believe that in theory the Java language specification and the Java Virtual Machine specification would allow an implementation to evaluate every case one-after-another. In practice if you have more than two cases the compiler and virtual machine are going to use a much more efficient method; I would be very surprised if using an array is faster. Check out the notes on compiling `switch` statements in the [Java Virtual Machine Specification](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10). – Samuel Edwin Ward Feb 02 '13 at 01:46