Variable Name as Object Name

How do I define a name for a new instance of a class using a variable content as the name?

so in the example below i want test to be the name of the instance created of the new class object, but for some reason it doesn't get the content of the string instead takes the string name.

String name ="test";

room name =new room();

Thanks

[425 byte] By [QTQa] at [2007-11-26 19:33:40]
# 1

I just cannot figure out what you are talking about. What do you mean by "name for a new instance"? The name of the class? The name of a variable referencing the instance? An instance of a class does not itself have a name, unless the class happens to contain a "name" variable...

The more I read your question, the more I think that what you are trying to do is to create a variable whose name ("test") is not known at compile time. You can't.

Message was edited by:

glevner

glevnera at 2007-7-9 22:06:21 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/docs/books/tutorial/reflect/index.html
Navy_Codera at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi sorry for the confusion i'm getting confused too the more I read up on this.

Basically I want to create a new Object with the name of the Object coming from a String.. so Is it possible for a User to enter a name in a textfield which is then stored in a string and that string is used to create the object..

so CLASS OBJECTNAME = NEW CLASS()

with the OBJECTNAME being a string, which contains the name..

Does that make more sense?

QTQa at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 4
Navy_Coder : thanks am reading up on it now hopefully gives me a better insight
QTQa at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 5

> How do I define a name for a new instance of a class

As glevner says instances don't have names, so you can't. When you say something like:String myStr = "hello world";

myStr is not the name of the string. It is a variable whose value is a reference to the string.

You can use a Map<String,Room> to associate a room with a string. And then your program can regard the associated string as a name for the room (and give that concept whatever meaning it wishes).

Or, as glevner suggests, the Room class could contain a string field that you reagard as the name.

But before you use maps (or reflection or anything else), you should think carefully about what you are trying to do and say what that is. At the moment your question focusses too narrowly on how.

pbrockway2a at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 6

> so Is it possible for a User to enter a name in a textfield which is then stored in a

> string and that string is used to create the object..

I still can't see what you are trying to do. An instance of the Room class will be created by using "new". new Room(etc) will return a reference to a newly created room. What role do you think this user entered string will play?

pbrockway2a at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 7

> Hi sorry for the confusion i'm getting confused too

> the more I read up on this.

> Basically I want to create a new Object with the name

> of the Object coming from a String..

Objects don't have names.

As suggested, use the "name" as a key in a Map. The value would be your Room object.

http://java.sun.com/docs/books/tutorial/collections/

That's what you'd do if you want to be able to look up a room by a "name". If you want the name to be an attribute of the room--as in, "every room has a name," then there should be a name member variable in the Room class. You might then do somethign like:Map<String, Room> roomMap = new HashMap<String, Room>();

...

Room room = new Room("My Room"); // assume the name member variable gets set to whatever we pass in here

roomMap.put(room.getName(), room);

...

Room roomToFindByName = roomMap.get("My Room");

jverda at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 8

> so in the example below i want test to be the name of

> the instance created of the new class object, but for

> some reason it doesn't get the content of the string

> instead takes the string name.

That "some reason" is that the Java language, by design, does not work that way. Variable names are known at compile time.

jverda at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 9
I have another issue String name = "test";room test= new room();is it possible to make the instance name vaiable
amgada at 2007-7-9 22:06:22 > top of Java-index,Java Essentials,Java Programming...
# 10

> I have another issue

>

> > String name = "test";

> room test= new room();

>

>

> is it possible to make the instance name vaiable

that's not another issue, it's the same issue. which has already been answered. variable names are not object names

georgemca at 2007-7-9 22:06:23 > top of Java-index,Java Essentials,Java Programming...
# 11
as previously above, you should use reflection:Class c = Class.forName("com.company.module.MyClassName");
java_2006a at 2007-7-9 22:06:23 > top of Java-index,Java Essentials,Java Programming...
# 12
> as previously above, you should use reflection:> [code]Class c => Class.forName("com.company.module.MyClassName");[/codethat's not reflection, and it doesn't address this non-problem
georgemca at 2007-7-9 22:06:23 > top of Java-index,Java Essentials,Java Programming...
# 13
>that's not reflection !!!!!!! http://java.sun.com/docs/books/tutorial/reflect/class/getClass.html
java_2006a at 2007-7-9 22:06:23 > top of Java-index,Java Essentials,Java Programming...
# 14

> >that's not reflection !!!!!!!

> http://java.sun.com/docs/books/tutorial/reflect/class/

> getClass.html

and? they also mention java.awt.Button in there. is that reflection? Class.forName is a way of getting a class object, upon which to reflect, but it's not really reflection itself. and doesn't bear relevance to the thread, anyway, which was about mistaking variable names for object names

georgemca at 2007-7-9 22:06:23 > top of Java-index,Java Essentials,Java Programming...
# 15

> I have another issue

>

> > String name = "test";

> room test= new room();

>

>

> is it possible to make the instance name vaiable

There is no "instance name." Objects don't have names.

jverda at 2007-7-21 17:37:34 > top of Java-index,Java Essentials,Java Programming...
# 16

> > I have another issue

> >

> > > > String name = "test";

> > room test= new room();

> >

> >

> > is it possible to make the instance name vaiable

>

>

> There is no "instance name." Objects don't have names.

just to flog this dead horse even further, consider the following code

Object first = new Object();

Object second = first;

System.out.println(first == second);

first = null;

second.hashCode();

the above prints out "true", so both first and second refer to the same object. now, does that one object have 2 names? no, it doesn't. it has none. elsewhere in our code, we could again assign a totally different variable to that same object, and the same would still stand true. we can choose to use either 'first' or 'second' to deal with that object, but they are just handles to the area of memory where the object lives, not names. further, we can see by the lack of NullPointerException that the object created is independent of, and not aware of, the variables that point to it. we can see that the variables which point to an object are not the object itself. so for the umpteenth time, objects do not have names

georgemca at 2007-7-21 17:37:34 > top of Java-index,Java Essentials,Java Programming...