JAVA Create object with variable name

Ok, this seems to be an age old question, evidently. Is it possible to create / reference an object in Java using a variable name?

For example:


String objName = "object1";
carObject objName = new carObject;
objName = "object2";
carObject objName = new carObject;
object1.speedUp();
object2.speedUp();
 

Now, that won’t work for obvious reasons. But is there any way I could accomplish the effect I’m going for here?

I suppose you could make a method that you pass the variable to:

carObject object1 = new carObject;  
carObject object2 = new carObject; 
speeder(object1);
speeder(object2);
void speeder(carObject objectName) {
objectName.speedUp();
}

The way you want to do it doesn’t work. If you gave us the situation you wanted to use it in it’d probably make a lot more sense.

I’m not exactly sure what you want to achieve. The normal Syntax to create a new Object is as following:

class temp = new class(); 

A class defining a constructor may need parameters to be handed over.
“temp” is now your reference to the newly created Object. You can access the class’ methods by the normal “temp.doSomething()” syntax.

You are creating a new String object and then you want to force another type onto it! As long as the new type is NOT one that extends on the previous class this operation won’t be allowed.

There’s no point in defining an additional method to execute one you could as easely access as it is:

carObject object1 = new carObject();
carObject object2 = new carObject(); 
object1.speedUp();
object2.speedUp(); 

Right. I’m trying to take the text of the string and make it be the name of the new object, not making the string become the new object.

Sorry if this doesn’t make sense, guys. But thanks for the help thus far! I’m not really applying this to anything yet, but my teacher wanted to know and I get extra credit if I can show him how.

^ Changing the name of the Object? Would only be possible to do by handing it to another variable:

Say you want “object1” to be renamed to “object2”:

carObject object1 = new carObject();
carObject object2 = object1; 

Though this not a rename, you will end up with two references to the same object.

I don’t see the point in that, sorry :stuck_out_tongue:

EDIT: It doesn’t make much sense as you can name it all you like when you’re actually typing code. So, my more sensical advice would be to highlight the name of your variable, press delete and finaly type in what you want it to look like :slight_smile:

Expanding even more on that: once you create an object, you can not alter the name of the reference to it. It will eventually be recycled once the app detects it’s of no use anymore.
It would be a real nightmare if it were possible considering other objects may point into the void.

Lol. Ok I’ll give an example.
Say a user inputs a number, and I want the program to create that amount of objects with a new name for each object.
i.e.

public static void main(String[] args) throws IOException {
	String output;
	int x = 0;
	inNum = consoleReader.nextLine();
	for(x=0;x<inNum;x++){
		carObject ("obj"+x) = new carObj;
	}
	inNum = consoleReader.nextLine();
	("obj"+inNum).speedUp();
}

The string value of (“obj”+x) or (“obj”+inNum) would be parsed as the name of the object; I’m not trying to run the “speedUp” method on the string itself, just the object that’s named the value of that string.

So, if the first number they inputted was 2, then 3 objects would be created:

obj0 obj1 obj2

If they inputted 2 as the second number, then it would be the same thing as

obj2.speedUp();

Does that make things a bit clearer? I so suck at explaining things. Sorry D;

what about arrays?

	for(x=0;x<inNum;x++){
		carObject obj[x] = new carObj;
	}

By George, I think you’ve got it!
I’ll see if this counts. I can’t believe this simple solution evaded me!
Thanks!
-Dillon

Yep, that’s what I would have suggested aswell.

First you would need to declare it:

carObject[] obj = new carObject[maxlength]; 

“maxlength” should be the max number entered as argument when parsing.

The for loop:

for(int x=0;x<inNum;x++){
        obj[x] = new carObject();
    } 

EDIT fixed “i” instead of “x”

Yes! I got the extra credit! Thank you guys so much!

I was just thinking that right after I read the above post.

Yeah I was required to run the loop to make them all new objects, I found.

To answer your question more specifically, you can do what you ask (to create an object reference more dynamically) using reflection, but to make use of it you still need to store it somewhere.
To use “dynamically named storage” in a completely general sense, I’d recommend (assuming this is all in the context of your console app) using a HashTable to assign names to objects.
Putting all that together you’d get the ability to create a new object from an arbitrary type and store it under an arbitrary name.

Yep, that’ll happen, because you could initialize them each to use different constructors or variables or something if you wanted.

Founded in 2004, Leakfree.org became one of the first online communities dedicated to Valve’s Source engine development. It is more famously known for the formation of Black Mesa: Source under the 'Leakfree Modification Team' handle in September 2004.