Search code examples
javastringextend

Extending Object Titles with Strings


I have an object, of which I want the name to be randomized like this:

Object object = type_""+rand(1,4);

so "object" could be object_1, or object_2, etc. (they're defined somewhere else) But this only seems to work if object is a String. How does it work for other types, too?

To make it more clear, here is an example, whcih creates a Sprite (in andengine):

super(2, 900, ResourcesManager.getInstance().box_1 , vbo);  

But box_1 is randomized..could also be box_2, box_3...How can I achieve it?


Solution

  • Completely rewritten based on the update to the question

    You can do this using Strings, but it's a bit hacky:

    super(2, 900, ResourceManager.class.getDeclaredField("box_" + (1 + new Random().nextInt(2))).get(ResourcesManager.getInstance()) , vbo);