-2

As my title states I am trying to dynamically reference an instance of an object by a string. Since I likely do not use the exact correct technical terms, here is basically the starting situtation.:

MyClass instance123 = new MyClass();
String referer = "instance123";

What i want to do it, refer to the object instance123 by the String referer.

E.g.:

callObjectByString(referer).anyMethodOfMyClass();

Which i would normally call like:

instance123.anyMethodOfMyClass();

I hope this is somewhat understandable and possible. I know that there is always a different way to program amd I could surely work around this problem somehow but I still want to find a solution to this problem!

C. Egger
  • 1
  • 2
  • Why do you want to do this? An object has no name. Local variables cannot be retrieved by name. Instance and class variables can, through reflection. It's unclear what you need at the moment. – Sotirios Delimanolis May 23 '16 at 18:59
  • 3
    Related: http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java – Sotirios Delimanolis May 23 '16 at 18:59
  • Mhm ok thank you for your answer. I am trying to refer to an instance fo a class by a string. I have looked at reflection but only found, that i can refer to a method by string. Or did I miss something? – C. Egger May 23 '16 at 19:01
  • 2
    That's not the Java way. What's the underlying problem you're trying to solve? – Aaron May 23 '16 at 19:02
  • No, you are trying to refer to a variable. A variable and an instance are two different things. – Sotirios Delimanolis May 23 '16 at 19:02
  • 3
    Couldn't you just use a ```Map``` ? – Jorn Vernee May 23 '16 at 19:03
  • Please look up java.utiil.Map –  May 23 '16 at 19:03
  • @Aaron The underlying problem is 2 classes that should "contain" each other. Class Line and Class Stations. Class Line should be able to add stations (which it does in it's constructor) and Class Stations should be able to be assigned to a Line. – C. Egger May 23 '16 at 19:15
  • Maybe I should instance the Class Line without the constructor forcing me to add Stations in the bginning, so that i can first construct the Stations (which would now assign Line(s)) and then add the properties of the Lines with methods.. – C. Egger May 23 '16 at 19:19
  • Then your `Line` class should have a `Collection` (possibly a `List` or `Set` depending on whether it should be sorted or not) and your `Station` class either a single `Line` field if it can only serve a `Line`, or another `Collection` – Aaron May 23 '16 at 19:20
  • Yeah, you have to solve the chicken/egg problem and instantiate one of the classes without a reference to the other, that will only be added later when the other class is constructed (I don't know if any one calls that the chicken/egg problem, but what I mean is that one must come before the other) – Aaron May 23 '16 at 19:21
  • @Aaron Yes i solved this problem now, by removing Station as a parameter from Line's constructor. Now i simply make an instance of Line, construct the Station (that has Line as a parameter in its constructor) and then call something like Line.addProperties(Stations) – C. Egger May 23 '16 at 19:35
  • Looks quite good to me, just two notes : 1) `Line`'s method could be more explicit, why not `Line.addStation(Station s)` ? If you have a `List properties` field it will be harder to work with if/when you need to retrieve the stations. 2) the call itself can be done from the `Station`'s constructor, if that's not what you've already done. – Aaron May 24 '16 at 08:50
  • 1) Line method is called addStations(Station[] s). It added other properties as well, earlier, thats why it was called addProperties(Station s, String name). But the string is added by the constructor now :) 2)Since only accept Arrays of Station, i cannot call addStations in the Station constructor. – C. Egger May 24 '16 at 17:04

2 Answers2

1

Here is the use of Map people are referring to in the comments :

Map<String, MyClass> myInstancesCollection = new HashMap<>();

MyClass instance123 = new MyClass();
String referer = "intance123";
myInstancesCollection.put(referer, instance123);

MyClass instance124 = new MyClass();
referer = "intance124";
myInstancesCollection.put(referer, instance124);

//later on...
myInstancesCollection.get("instance123"); // to retrieve the instance123 object
myInstancesCollection.get("instance124"); // to retrieve the instance124 object
Aaron
  • 24,009
  • 2
  • 33
  • 57
0

Why not do this?

Inside of MyClass you can make an "id" field, like this:

public MyClass{
  private String id;
  public MyClass(String id){
    this.id = id;
  }
  public getID(){
    return id;
  }
}

Then do,

String refer = "test";
MyClass test = MyClass("test");
if(test.getID().equals(refer)){
  //do something with the object test
}

And if you have a List of objects:

ArrayList<MyClass> objs = new ArrayList<>();

objs.add(new MyClass("obj1"));
objs.add(new MyClass("obj2"));
objs.add(new MyClass("obj3"));

String refer = "obj2";

for(int i = 0; i < objs.size(); i++){
  if(objs.get(i).getID().equals(refer)){
    //do something with objs.get(i)
  }
}
Arman
  • 655
  • 2
  • 7
  • 23
  • Would probably work in some cases, but it won't work with my underlying problem. But i thank you a lot for your help! – C. Egger May 23 '16 at 19:17