Andrew snowfall
Andrew snowfall

Reputation: 1

Getting a variable using a variable

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

Answers (2)

voidMainReturn
voidMainReturn

Reputation: 3507

how about using class.forName :

Object o = Class.forName(userclass)

this assuming your userclass is a string.

Upvotes: 0

zw324
zw324

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

Related Questions