Reputation: 1
How would I get a variable in another class using a variable in the current class? For example: The variable "userclass" Can either be human or alien. Inside my other class ("Cv.java") there are two variables (human, alien) How would I get one of the two variables in Cv.java, while using the "userclass" variable to get it.
Example 2:
userclass = alien
Cv.????
How would I get Cv.alien whilst using "userclass"
Upvotes: 0
Views: 106
Reputation: 3507
how about using class.forName :
Object o = Class.forName(userclass)
this assuming your userclass is a string.
Upvotes: 0
Reputation: 27190
You can use reflection to do it, although it might hurt the performance.
A better way is to use a Enum
to represent the userclass
instead of using a String
.
Upvotes: 1