OOP_Noob
OOP_Noob

Reputation: 1

How can I reference an instantiated object with only a string representation of its name?

Let's say I have an instantiated object:

private static ArrayList<Boolean> P1SOLUTION = new ArrayList<Boolean>();

There will be similar objects such as P2SOLUTION, P3SOLUTION, etc. I want the functionality of:

Arrays.toString(P1SOLUTION);

(Which prints the array as a string). But let's say all I have is...

String myString = "P1" + "SOLUTION";

So, when I invalidly write:

Arrays.toString(myString);

I really want the String myString to reference the object P1SOLUTION in this example. How can I create this functionality?

Upvotes: 0

Views: 145

Answers (1)

sourcerebels
sourcerebels

Reputation: 5190

Store your object instances in a Map. Then reference the instances by name:

Something like this:

Map myMap = new HashMap();
myMap.put("P1SOLUTION", new ArrayList<Boolean>());

Then get your instance:

String myString = "P1" + "SOLUTION";
List myList = myMap.get(myString);

Hope this will help you.

Upvotes: 2

Related Questions