1

Is there a method that exists that allows me to get the name of a field in another object?

AMDG
  • 1,118
  • 1
  • 13
  • 29
  • If I understand you correctly, an object does not belong to a class. A variable belongs to a class or some other scope. If you are using it, you already know what class or scope that is. – Sotirios Delimanolis Apr 25 '14 at 22:29
  • yes, * I do*, but if its just a list in the whole output saying things like: null initialized. false iniatialized. java.lang.Integer initialized. value: 0. – AMDG Apr 25 '14 at 22:31
  • You refer to the 'objects own name', if this is not the class name, what do you mean by this? – GoldenJam Apr 25 '14 at 22:34
  • What I mean is the identifier. `Object *obj*;` obj is what I need to know what initialized in the class which is ? but I dont know what it is! – AMDG Apr 25 '14 at 22:47

2 Answers2

2

If I understand your question correctly, you can (mis)use Java's Stack Trace system to determine the caller. Put something like this in the constructor of an object:

try {
        throw new Exception();
    } catch (Exception ex) {
        String s = ex.getStackTrace()[1].getClassName();
        System.out.println(s);
    }

I doub't that's EXACTLY what you are looking for, but, if you play around with it a bit, I think you'll find what you want.

Cody S
  • 4,744
  • 8
  • 33
  • 64
  • I must say, quite clever really. (misuse or not :)) – takendarkk Apr 25 '14 at 22:32
  • @Takendarkk apparently not clever enough. I've been downvoted it seems. – Cody S Apr 25 '14 at 22:35
  • Well, it might not work correctly or something along those lines, I'm not real sure. But I liked the train of thought. – takendarkk Apr 25 '14 at 22:36
  • @Takendarkk Incidentally, I ran this and it worked great, with the caveat that it prints `package.path.classname` instead of just `classname`, but other than that, it accomplishes what (I think) he wants. – Cody S Apr 25 '14 at 22:37
  • 1
    It seems he is looking for something like this: If he wrote `Obj myObj22 = new Object()` he wants to be able to find `myObj22`. – takendarkk Apr 25 '14 at 22:49
  • hm yes that helps for the class part! thanks, but is it going to throw that exception and then end the program? I just want the name so I know what object was initialized. – AMDG Apr 25 '14 at 22:49
  • @LinkTheProgrammer No, the program won't end...That is a confusing comment, I'm still not sure exactly what you are looking for. Sorry pal. – Cody S Apr 25 '14 at 22:54
  • OH THANK GOD! So if it won't end then that will definitely help! also, @Takendarkk is right, look at his comment, he knows exactly what I want! – AMDG Apr 25 '14 at 23:00
  • For example, you have `Object bar = new Object(); Object foo = null; foo.equals(bar);` this line would raise a `NullPointerException`. OP wants to get something similar to *Variable `foo` is `null` and will throw a NPE.* – Luiggi Mendoza Apr 25 '14 at 23:00
  • @LinkTheProgrammer Oh. Yeah. Sorry, nope, don't know how to do that (or if it's even possible). – Cody S Apr 25 '14 at 23:02
  • @CodyS it is impossible to accomplish that. The most you can get is the current line of execution of the class using the way you posted. I tried to do something similar and searched all over the net to understand this is the most that you can get (and sadly, it is considered a hack). – Luiggi Mendoza Apr 25 '14 at 23:04
  • Who cares about hacks so long as you *drink responsibly* (pun intended) I hack for good causes, not to ruin people's lives... – AMDG Apr 25 '14 at 23:26
  • @LinkTheProgrammer because hacks are not well suited in real world programming. – Luiggi Mendoza Apr 25 '14 at 23:30
  • @LuiggiMendoza I concur. Even so, I would use it anyways since its the ONLY way and there is no alternative except for some really Inefficient *thing* that goes into the RAM and looks for and matches each object with a name within your program. – AMDG Apr 25 '14 at 23:37
  • @CodyS You're a life saver! This is what I've been looking for!!! It gives the actual data location not the field name, but its good enough! – AMDG Apr 26 '14 at 03:12
  • perhaps I should just add an array to say that a certain data member was initialized: `String[] objIdentifier ={this.getClass().getCanonicalName() +".obj",...};` – AMDG Apr 26 '14 at 03:23
  • I don't think anyone realizes just how overjoyed I am! Initializer.class is finally complete thanks to you! I'm one step closer to an Open-Source game library for 2D and 3D! EDIT: thank you stackoverflow (the members that contributed)! <3 – AMDG Apr 26 '14 at 05:02
  • lol guys, you didn't know about getClass().getFields() will show the field names and getDeclaredFields() will show the FULL names! Thats what Im guessing... just thought I would mention it! – AMDG May 06 '14 at 00:40
2

There is no way to do this, period. An object can be simultaneously referred to by many variables, and there is no "master list" you can query. The information you want is simply not available. Objects don't have "names."

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Perhaps I should edit this as it is causing misinterpretations apparently. What i want is the identifier it goes by. any kind of object has a special "name" and by that name I mean the computer goes by some kind of location. The name here is if you have `Object apple` then apple is the "name" of the object. – AMDG Apr 25 '14 at 22:51
  • 1
    @LinkTheProgrammer Ernest is correctly stating that there is no such identifier, there is no such location, not using Java, not even using reflection. For the JVM, there's no `apple` anywhere, just a *random* space in memory being used. – Luiggi Mendoza Apr 25 '14 at 22:56
  • then how exactly does the debugger in your IDE work when it lists all your variables and objects as well as class names! Perhaps there is some native method i can implement into the program that will get those Object names for me? `private static native void getObjectName()` ? Then again its probably using txt recognition and just finding things after a declaration I guess no? – AMDG Apr 25 '14 at 23:04
  • 1
    @LinkTheProgrammer it is more complex than that. But you may look to [JPDA](http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/jpda.html). – Luiggi Mendoza Apr 25 '14 at 23:06
  • also, there has to be *some* kind of "name" or better yet, place, that the JVM needs in order to get to the object! I mean how else would it work? It needs some sort of as stated so that it knows where in the memory it should look for it does it not? – AMDG Apr 25 '14 at 23:06
  • The "name" you speak of is just so us puny humans have something to read, nothing more. – takendarkk Apr 25 '14 at 23:09
  • @LinkTheProgrammer the JVM does not need such things as variable names. You can note it by checking the generated bytecode of your classes using `javap -c DesiredClass.class`. – Luiggi Mendoza Apr 25 '14 at 23:10
  • @LuiggiMendoza +1 indeed. This seems to be perfect for what I need. Apparently its just a matter of good old sun's libraries and their "unsafe" code to give exactly what I need, and it was just made for debugging! Perfect! I don't think this can any better. Thank you so much! – AMDG Apr 25 '14 at 23:12
  • @LinkTheProgrammer you're welcome. But note that it is unsafe, so it would be better to not use it in production environment. – Luiggi Mendoza Apr 25 '14 at 23:13
  • I was referring to a class that is probable to leave the libraries which is somewhere in sun `.Unsafe` and deals with alot of memory manipulation. And is what I need indeed unsafe? its just getting some names; what harm is that? – AMDG Apr 25 '14 at 23:16
  • @LinkTheProgrammer those are pretty obscure classes that should not see the light. Not joking. Note that you cannot even have easy access to them (at least I couldn't, I needed to somewhat modify a JDK installation to make them work). – Luiggi Mendoza Apr 25 '14 at 23:19
  • All I did was import it, `Unsafe unsafe = null;` and then I used it. Nothing to it really... EDIT: the page is expired: com.sun.jdi doesn't exit in Java 1.8; It turned to com.sun.jndi with new folders in it! – AMDG Apr 25 '14 at 23:23
  • 1
    None of this changes the fact that given an object, there is no way to get the "name", defined as the name of the variable that points to it. A moments thought will prove that this must be true, because a single object could be referred to by 1000 variables, all with different variable names. Neither Unsafe nor JPDA will give you this info, because *it does not exist.* – Ernest Friedman-Hill Apr 26 '14 at 01:40
  • If you want a semi-unique identifier for an object, check out System.identityHashCode(). Not particularly human-readable, however. – Ernest Friedman-Hill Apr 26 '14 at 01:45
  • oh so is that like the thing I commented recently below? (@10dea4e?) – AMDG Apr 26 '14 at 03:28
  • thanks for the clarification Ernest; The Field memory location identifire `@10dea4e` and etcetera for other fields is good enough. Its more accurate I think than the hashcode, unless of course by some chance that (lol) its just the identifier turned to numbers (which it isn't). – AMDG Apr 26 '14 at 05:07