user702026
user702026

Reputation:

How do I take a value from variable and assign it asa a new object name?

I have a function that gets an int value from the user and assigns it to an int id. I have a Paper class. So what I want to do is everytime I get value from user I want to create a Paper object with that name.

For eg.

// get value from user and assign it to int id;
id= 312;

// create Paper object with value from id
Paper (value of id) = new Paper();

Upvotes: 0

Views: 60

Answers (2)

manocha_ak
manocha_ak

Reputation: 902

I wondered to achieve this thing in my school days too :). Part of learning curve. 9 out of 10 times, there is no need of such thing. For that rare 1, keep it as part of your class. (I mean applicable only if its part of your domain)

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Variable names are a compile-time thing; they don't exist at run-time. You probably want to use something like a map:

Map<Integer,Paper> = new HashMap<Integer,Paper>();

...

map.put(id, new Paper());

Upvotes: 4

Related Questions