user1633277
user1633277

Reputation: 510

JAVA Using a variable value as an Instance name

So basically Im reading a file , each line of this file contains description for from 3 to 5 instances of two types of objects with 2 to 4 being first type of object and last one being another type . The file can contain up to 500 lines . I am going to reuse each object from 1 to unknown but on scale of hundrets of times and I need to track their status .

While I can all the data into an array and use only 5 objects changing their values constantly that will make it rather hard to track status of each combination of their parameters .

What I want to is to create my instances with names such as FromLine1Obj1 , FromLine10Obj3 .

I failed to mention the fact that each set of objects from a single line should also create a new thread and this set is proccesed in that thread .

Upvotes: 0

Views: 234

Answers (4)

jdevelop
jdevelop

Reputation: 12296

If you read the file once and it will not change after all - you may want to do some code generation, like http://cglib.sourceforge.net/

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533442

You need to use a Map.

Map<String, MyObject> map = new ....

map.put("FromLine1Obj1", new MyObject());

MyObject mo = map.get("FromLine1Obj1");

Upvotes: 4

jolivier
jolivier

Reputation: 7635

You can store your parsing result in a map, with as key the name you want and as value the created instance, that's the clean way around name indirection in Java.

Upvotes: 0

Dan
Dan

Reputation: 1018

Could you not use a map with the name as the key?

Upvotes: 0

Related Questions