Adam Outler Adam Outler - 5 months ago 10
Java Question

how to call a java method using a variable name?

Say I have Method1(void), Method2(void)...

Is there a way i can chose one of those with a variable?

String MyVar=2;
MethodMyVar();

Answer

Use reflection:

Method method = WhateverYourClassIs.class.getDeclaredMethod("Method" + MyVar);
method.invoke();
Comments