• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get the current Object reference name?  RSS feed

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!!

I have a question about getting the current reference name, i know i can use getClass().getName() to get the name of the current class.
but how do i get the actuall name of that particular instance?



IS there a way to do that or must i create a method to do it? or is it possible at all?

Thanks
Mike
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no way to do this. The name you give the variable that refers to that object instance is not known to the object.

Consider that you could create multiple variables which ALL had references to the same object; the idea of "the name of" the object instance is meaningless, and this case illustrates that.

You can create a field in an object that contains a name, and assign that name when you create the object, and then retrieve that if you want to.



And then you can do:


And now, incidentally, if you create multiple variables that all have a reference to this object, they will all get the same name.

rc
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ralph, I figured each object instance had its own name so thought it could be done.

I suppose i could then use Scanner to create the new objects then they would all have a unique name.

Thanks for the reply!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference names are really only useful to the programmer. the JVM (i believe) hashes them out to something that it prefers (I could be wrong on that).

But even if you did get all the names, I don't see what good it would do you. To create the new name, you'd need a variable to hold THAT, which you'd have to know the name of in advance, unless you wanted to create IT dynamically, in which case...

If you want to create a bunch of new objects, you can use a collection - like an ArrayList, or a Map, or something else. That is much better than trying to come up with a new reference name. Plus there is the fact you can do this:

Boat fredsBoat = new Boat();
Boat staceysBoat = fredsBoat();

The object has two reference variables that point to it - what would be the right name? What happens if we then do this:

String boatOwner = getName(); //Say this returns "fredsBoat
fredsBoat = new Boat(); //Yay!!! I got a new boat!!!

paintBoat(boatOwner); //You just painted my old boat!!!
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

I think i understand now, thanks for your help in clearing that up for me.

Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!