How do I print the variable name holding my object?
For example, I have:
myclass ob=new myclass()
How would I print \"ob\"?
Try reflection + hash code. E.g.:
String hashcode = ob.hashCode();
String obName;
java.lang.reflect.Field[] fields =
ob.getClass().getDeclaredFields();
for( final java.lang.reflect.Field field : fields )
{
if( field.hashCode().equals(hashcode) )
{
obName = field.getName();
break;
}
}
System.out.println(obName);