Adam Outler Adam Outler - 1 year ago 76
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;

Answer Source

Use reflection:

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