Adam Outler Adam Outler - 1 year ago 96
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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download