• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get the current Object reference name?

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred,

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

Mike
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this possible in current vsns of Java?

I would like to be able to reference the name of instances of a class because I'm working on a project where each object is ai different invoice, and I'd like to use the object name as the invoice number.

So if I have

and I thought I'd write:

of course I was thinking invName could be my String variable to pass the object's name to and use it as the invoice number.

Is there a better way to do this? I did read the other posts on this thread and many on the internet, so that's where I'm at.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a Map to associate a String with an Object:  Map<String, Object>  or Map<Object, String>
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rich Mai wrote:Is this possible in current vsns of Java? . . .

No.

There isn't a concept that there is a “name” for a particular object; the names you give to identifiers are erased and they are never used at runtime. Nor can you use the memory location of the object because
  • 1: it is not accessible; not even System#identityHashCode() can be relied on to give you the memory location.
  • 2; the memory location can change with garbage collection.
  • If an invoice has a serial number, then that serial number would be a field of the Invoice object. That would be the object‑oriented way to do it. This is a naïve implementation:-I can see all sorts of problems with that approach, but don't think any of those problems is insoluble:-
  • 1: That technique isn't thread‑safe. You might be able to sort that out by making the static field volatile.
  • 2: If you run the code several times, you will always start from the same serial number. There are, however, all sorts of ways to store and restore a last serial number.
  • 3: If you run several JVMs simultaneously, there is a risk of several Invoices in different JVMs having the same serial number.
  • Another way to do this is to run a database program, inserting the values into an invoice table which has a serial_number as a column. If you use a transaction for this insertion and make the serial_number an “autoincrement”‑ing datatype, that will probably solve all those problems. But it may also be too complicated for your current requirements.
    You can use a Map<String, Object>, but I can foresee problems with a Map<Object, String>. If the “K” in a Map is mutable, and changes its state, its hash code will change and the chances of finding your “V” from it will be negligible.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I didn't at first notice that this is your first post: welcome to the Ranch
     
    Rich Mai
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    If an invoice has a serial number, then that serial number would be a field of the Invoice object. That would be the object‑oriented way to do it. This is a naïve implementation:-
    ...
    I can see all sorts of problems with that approach, but don't think any of those problems is insoluble:-
    .....



    Thanks Richie,

    That's what I was getting at, an articulated solution--if I had time to get serialization working, I'd run with it. And I like your speculation about using a database; in case this was a work project and multiple users would need to keep track of serialized invoice numbers--I will need to figure out something like that in the future. But for now, since this is just an assignment, I lazed out of creating invoice identifiers by asking the user to create and enter a name (which, in my defense, custom invoice naming is standard for programs from Quickbooks to online retailers who allow invoice services--but of course they also keep the last used numbers for reference...).


    Thanks again for showing an approach to serializing. I'm going to tuck this away for the future.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's a pleasure

    Please don't call it serialising; that means something different. You'll find a little more information here.
     
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Consider:

    so if the were possible to get the variable name given an object, what would be the result? There are two references to the same object. There can be as many as memory allows.

    That's why this isn't even a concept.

    When I need to keep track of objects given some sort of id, I always make that id part of the object. And if I need to look them up quickly, I'll create a Map if the id to the object.

    This also lends itself well for the future improvements. If the object are to get associated to a relational database table, the id field makes a natural primary key.
     
    Rich Mai
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Consider:

    so if the were possible to get the variable name given an object, what would be the result? There are two references to the same object. There can be as many as memory allows.

    That's why this isn't even a concept.
    ...
    If the object are to get associated to a relational database table, the id field makes a natural primary key.



    There's that 'map' word again. Sounds like I need to get that figured out. Because I understand databases much better than Java,  I like when you say "... the id field makes a natural primary key".
     
    Norm Radder
    Rancher
    Posts: 5008
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Our use of Map refers to a class.  The API doc: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
    https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic