Pointing to instance methods from a static table in Java

I want to have a table sort of like this:

static final Record [] table = {
    new Record( Pattern.compile( "regex1" ), MyClass::f1 ),
    new Record( Pattern.compile( "regex2" ), MyClass::f2 )
};

where f1, f2 etc are references to instance (not static) methods with an argument and a return value each like this:

public int f1( int arg ) {
    return arg * arg;
}
public int f2( int arg ) {
    return arg + arg;
}

so I can invoke them something like this (pseudocode):

void foo( String s, int arg ) {
    for( Record r : table ) {
        if( r.regex.matcher( s ).matches() ) {
            int result = r.func.invokeOn( this, arg );
            break;
        }
    }
}

What is the correct way of declaring the second argument of the Record constructor, aka member variable func in the pseudocode? I figured it out for static f1, f2 etc, but I get a variety of rather incomprehensible error messages regardless what I try for non-static declarations of f1 etc. I assume it can be done?

Answer

You can use BiFunction<MyClass, Integer, Integer> assuming MyClass is the class containing the methods (or use an Interface instead of a class). A method reference to a non-static method without an instance (only the class is given), kind of introduces1 an additional parameter representing the instance when the method is executed (this) as first parameter.

class Record {
    BiFunction<MyClass, Integer, Integer> func = MyClass::f1;
}

for

class MyClass {
    public int f1(int arg) {
        return arg * arg;
    }

    int example(Record r, int arg) {
        return r.func.apply(this, arg);
    }
}

1 – actually the this parameter is already in the bytecode of all instance methods (there is just no need to declare it. The f1 method is equivalent to public int f1(MyClass this, int arg).