-3

I want to make a String from the name of an Object

for example:

Object objectName = new Object;

Now I want a String made of the name of that Object so that I can print it

System.out.println(stringWithObjectName); \\ print objectName

How do you do that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Leonardo T.
  • 87
  • 10
  • 1
    Are you asking ```toString()```.......? – lier wu Feb 19 '21 at 20:55
  • @lierwu toString() gives you the id of the Object and not the name – Leonardo T. Feb 19 '21 at 21:04
  • I mean, you're the one who gives the object its own name, so... You can store the name and use ```toString``` to print...? – lier wu Feb 19 '21 at 21:08
  • 3
    An object has no "name". So either `objectName` is just an Object and the answer is that it's not possible, or it's of a specific type for which `name` means something and you need to be more specific in your question... – assylias Feb 19 '21 at 21:09
  • 1
    @assylias I think he means like, if you have ```int num = 1```, when you do ```System.out.println(num)```, he wants ```num``` to be printed? And it seems weird since he is the one who give the name. – lier wu Feb 19 '21 at 21:11
  • 2
    What you have is the name of a variable, not of an Object. objectName can point to many different Objects over it's lifetime. – Turo Feb 19 '21 at 21:11
  • 2
    @lierwu I see, you're probably right. This may help: https://stackoverflow.com/q/744226/829571 – assylias Feb 19 '21 at 21:15
  • You have to learn what the difference is between an *object* and a *variable*. Objects don't have names; variables have names. Many variables can refer to the same object, there is not a one-to-one correspondence between objects and variables. See [What is the difference between a variable, object, and reference?](https://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference) – Jesper Feb 19 '21 at 22:46

2 Answers2

4

That's.. not how it works. You have a fundamental misunderstanding of how java works. In:

Object name = new Object();

you did not just create a new object named name. No, you did 3 completely seperate things:

  1. You made a variable, of type Object, with name name. This is like a blank piece of paper that you can paint treasure maps on. That's the Object name; part.

  2. You made a new object. (The new Object() part). It has no name. It is like creating a new treasure chest, and burying it in the sand.

  3. You have drawn on your treasure map, the route to the treasure you just buried (that's the = in the middle part).

You can now tear up your treasure map (name = null - there, wiped out the map!), but the treasure is still there, buried in the sand. If there are no other treasure maps in existence where X marks that treasure, eventually the garbage collector will dig it and get rid of it.

In other words, objects can exist where no variables are pointing at it.

You can also make more treasure maps and copy the reference:

Object name = new Object();
Object otherName = name;

Now you have 1 treasure (new Object(), with no name, and 2 treasure maps (name and otherName) that currently have drawn maps on them going to that treasure. But you can change that:

Object name = new Object();
Object otherName = name;
name = someOtherObject;

now the name of the map that originally had a map to this object on it, now has a map to some completely different treasure on it.

Your question is: How can I ask the treasure about the name of the map that points at it. Hopefully this explanation makes it clear as to why this is not possible at all, and java does not let you do this, in any way.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I know what the garbage collector gets rid of objects without reference, and you confirm this by saying "_eventually the garbage collector will dig it and get rid of it_" , but than you say "**_In other words, objects can exist where no variables are pointing at it_**" ... you mean ** _can't exist_** or am i missing somethig? – Leonardo T. Feb 20 '21 at 12:01
  • @LeonardoT. They can and do exist, all the time. The garbage collector does not work at instantaneous speed. Your VM will have objects, eligible for collection, and they will (eventually) be collected - but not right away. There'd be no way to get at them, so whether they 'exist' or 'do not exist' is mostly moot (one of those 'if a tree falls in the forest and nobody hears it' situations), but it's there, in heap memory, and would work fine, if only you could wave a magic wand and get a reference to it (which you can't). Hacks with c.s.m.Unsafe and native could get you there, though. – rzwitserloot Feb 20 '21 at 16:42
  • Thanks for the detailed explanations, for me as a student it has been really helpful – Leonardo T. Feb 20 '21 at 20:51
1

Variable names are erased at runtime. But one way you can do this is using a map.

Map m = ...;
m.put(object, objectName);
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80