How do I print the variable name holding my object?
For example, I have:
myclass ob=new myclass()
How would I print "ob"?
How do I print the variable name holding my object?
For example, I have:
myclass ob=new myclass()
How would I print "ob"?
Objects don't have names, unless you happen to be using a class which allows each object to be given one (e.g. via a variable retrieved with getName()
).
In particular, the name of any particular variable used to refer to an object is completely unknown to the object itself. So you can't do:
Object foo = new Object();
// There's no support for this
String name = foo.getName(); // expecting to get "foo"
(Bear in mind that several variables could all refer to the same object, and there don't have to be any named variables referring to an object.)
To print the object type name:
System.out.println(myObject.getClass().getName());
Workaround. If you want the object name then you can initialize at constructor.
//in class myClass
private String objName;
myClass(String objN)
{
this.objName = objN;
..
}
public String getObjName() {
return objName;
}
//in main()
.
.
.
myclass ob = new myclass("ob"); //any other variables accessing this object
//eg. temOb = ob get the same object name "ob"
System.out.println("object name is: " + ob.getObjName());
myclass ob1 = new myclass("ob1");
System.out.println("object name is: " + ob1.getObjName());
System.out.println();
Is the command used to print out to the console.
So if you have your own class that you created and instantiated, you could do:
MyObject obj = new MyObject();
System.out.println(obj);
and that would print out the toString()
implementation of MyObject
. The default implementation is not very interesting, so for useful info, you would have to override toString()
.
Slightly more complete (but essentially the same as above):
object.getClass().getName()
will give you the fully qualified name of the object (i.e.package.className. For example, String thing = new String(); thing.getClass().getName() will return "java.lang.String".
className.class.getName()
will give you the fully qualified name of the object (i.e.package.className. For example. String.class.getName() will return "java.lang.String".
Print it how ever you want. Perhaps using System.out.println().
The name of the variable is compile time information that is not typically stored after compilation. The variable name is stored if you compile with debugging information.
In mamy contexts, it is a ridiculous request to want to print the name of a variable, but if you really need to do that, read up on java debugging information.
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);
I'm assuming you want a way to "print" an object, in which case, you'll first want to override the toString()
method for your object. This will allow you to specify what an object of your type returns when you call toString().
Then, you can simply do System.out.println(MyObject);
This will implicitly call MyObject's toString()
.
You can't get the name of object reference. Instead you can get its hashcode or counter. I solve this problem as follows:
class ObjectName
{
static int num;
ObjectName()
{
num++;
}
public int getObjectCount()
{
return num;
}
public static void main(String[] args) {
ObjectName ob1=new ObjectName();
System.out.println(ob1.getClass().getName()+" "+ob1.getObjectCount());
ObjectName ob2=new ObjectName();
System.out.println(ob1.getClass().getName()+" "+ob1.getObjectCount());
}
}
Output:
ObjectName 1
ObjectName 2
This achieves the required result, but it is a bit of a fudge, in that if the string returned by toString() is to actually reflect the name of your variable (which of course, only you, as the coder will know), you have to use the convention that the method uses i.e. in this implementation, because the returned string is in the format ClassName+counter, you have to name your variable using that format: myClass1, myClass2, etc. If you name your variable in some other way (e.g. className+A/B/C etc or someRandomVariableName) although the return string would remain as implemented by the toString() method i.e. returning myClass1, myClass2, etc, it wouldn't reflect the variable name you have actually used in your code.
class myClass {
private static int counter = 1;
private String string;
private int objCounter;
myClass(String string) {
this.string = new String(string);
objCounter = counter;
counter++;
}
@Override
public String toString() {
return this.getClass().getName() + objCounter + ": " + this.string;
}
public static void main(String[] args) {
myClass myClass1 = new myClass("This is the text for the first object.");
myClass myClass2 = new myClass("This is the text for the second object.");
System.out.println(myClass1);
System.out.println(myClass2);
}
}
import java.lang.reflect.*;
public class myclass {
private myclass ob;
public static void main(String args[]) {
Field fd[] = myclass.class.getDeclaredFields();
for (int i = 0; i < fd.length; i++) {
System.out.println(fd[i].getName());
}
}
}
I know it's too late)) but It here is a method like getSimpleName()
.
In this case :
myclass ob=new myclass()
System.out.println(ob.getClass().getSimpleName())
System output: ob