I think you can check this link out: Get variable by name from a String
There are four interesting suggestions there, using different approaches: Reflection, Map, Guava and Enum.
-------------- EDITED ANSWER -------------
(With contributions from David Wallace)
Please check the following program out:
import java.lang.reflect.Field;
public class Main
{
public final static int MN = 0;
public final static int PY = 1;
public final static int RB = 2;
private static int[][] myMatrix = new int[][]
{
{ 1, 2, 3 },
{ 10, 20, 30 },
{ 100, 200, 300 }
};
public static void main( String[] args ) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException
{
System.out.println( getElement( "MN", "PY" ) );
System.out.println( getElement( "PY", "RB" ) );
System.out.println( getElement( "RB", "RB" ) );
}
public static int getElement( String i0, String i1 ) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException
{
return myMatrix[ getValueByName( i0 )] [ getValueByName( i1 ) ];
}
public static int getValueByName( String varName ) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{
Field f = Main.class.getField( varName );
return f.getInt( f );
}
}
The output is: 2, 30, 300.
Does it respond your question?