Is there any way to define methods via method references, as demonstrated by the following imaginary code (and imaginary syntax)?
class A
{
public Sometype somefunction(Argtype arg)
{ ... return somevalue; ..}
}
class B
{
public Sometype method = A::somefunction;
}
or
class C
{
public Sometype method(Argtype arg) = A::somefunction;
}
A similar feature would seem quite useful to me. Methods are known at compile time, so any definition in class B
or C
might simply (after a type check) take over a reference to an already known method in A
. The advantage: faster calls (one less required than even in a direct call from a new method) - where I am not sure whether Java compilers do not optimize anyway?
Is there any way to achieve that already now in Java (if so, it has evaded my search successfully)? Or are there good reasons not to introduce that to Java? Are there any workarounds which provide similar performance advantage?
PS: I get the message that similar question titles have been downvoted and/or closed. If this applies here, too, please help me to understand why. Thanks in advance!