Reputation: 131
I want to display the variable name used for the instance of an object passed to a method. I can take the 'incoming' object and look at it's declared fields and see it's variable names, but I was wondering if there was an easy way of displaying that name.
I thought I could get a stack trace and get the class name of the calling class and then use a classloader and reflect that class's fields and methods looking for the same type but that is just messy and not foolproof.
The variable name I am looking for is not the one assigned in the method's parameter list, but rather the variable name used in the class that is calling my method.
For example
public class Ralph{
String punch = "mouth";
public void doIt(){
GetInfo.showIt(punch);
}
}
public class GetInfo{
public static void showIt(Object obj){
System.out.println("variable name = punch");
}
}
Obviously not compilable code but that's what I would like to be able to know in the showIt() method.
Upvotes: 0
Views: 1635
Reputation: 285415
What is the meaning of a "variable name"? What if your object has no named variable?
i.e., what if someone calls showIt("punch")
?
What if the object is referred to by two, three or more variables.
i.e., what if you have
String foo = "hello"
String bar = foo;
String baz = foo;
showIt(bar); // you're just passing in the same object referred to by 3 variables
This is an XY Problem if I've ever seen one. You should be focusing on and asking about the overall problem that you're trying to solve, not on your erroneous solution.
Perhaps you want to use a Map instead?
i.e.,
Map<String, FooType> fooMap = new HashMap<String, FooType>();
fooMap.put("Jim", new FooType("Jim"));
// later
FooType myJim = fooMap.get("Jim");
The key for Java is not to focus on variable names, for in compiled code, they almost don't even exist. Rather focus on references -- how are you going to get a handle on the object of choice. There are many ways of doing this including Maps if you want to associate an object with a String for instance, or arrays or ArrayLists to associate an object with an int.
Also, I see that you tagged your question reflection, but note that reflection won't help here. Again what you need to do is to re-think, re-design your program so that you don't need this requirement.
Edit
You state:
The problem I am trying to solve is to get the name of the variable. Sorry if "variable name" was vague. Maybe I should have called it field name. If I pass a class into my object I can get it's declared fields and do a field.getName() and see the name that was used in the code to declare that variable. So I am wanting to find the field name that was used for the object being passed to this discovery class. If that is not possible just say so.
Yes, as multiple folks have told you, this is not possible.
I think I stated my problem pretty plain. This is for debug/logging purposes.
No you haven't. You've stated and restated your problem with your code attempt but have not told us the rationale for wanting to attempt this approach. You're still pursuing the wrong horse and are still presenting us with an xy problem. What we need to know are the specifics behind this, the background behind your "This is for debug/logging purposes"
statement.
Upvotes: 4
Reputation: 16387
You can't do this. The variable names are not present at runtime.
Upvotes: 5