Set an array value equal to a variable in a dynamically loaded class in Java -
i have seemingly simple question have not been able find solution in week of searching, thought i'd ask. want take array value, anarray[1]
, set equal variable in class... otherclass.variableone
this easy do, want able use variable name of class want variable from. instead of "otherclass.variableone" want like:
string classvar = "otherclass"; anarray[1]=classvar.variableone;
i'm not sure whether need set classvar equal like... classname(otherclass)
or that. quite new java , i'm having great difficulty.
i know seems dumb necessary want do. have looked reflection doesn't seem able need. question is:
string classvar = "otherclass"; anarray[1]=classvar.variableone
how make work classvar variable refers class contains public static int variableone?
please excuse limited programming experience. appreciated. in advance.
you need use the reflection api. can use obtain class
variable representing "otherclass" (e.g., class.forname("com.example.otherclass"
) , go through fields , methods, , create new instances of it.
Comments
Post a Comment