Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print the variable name holding an object?

Tags:

java

How do I print the variable name holding my object?

For example, I have:

myclass ob=new myclass()

How would I print "ob"?

like image 280
Bipin Sahoo Avatar asked Oct 18 '10 12:10

Bipin Sahoo


People also ask

How do you print an object name in Python?

Print an Object in Python Using the __repr__() Method It, by default, returns the name of the object's class and the address of the object. When we print an object in Python using the print() function, the object's __str__() method is called.

Can you print a variable name in Java?

In short. You can't print just the name of a variable.

How do you reference an object name in Java?

The getName() method is used to get the names of the entities such as interface, class, array class, void etc. that are represented by the class objects.

How do I get the name of an object in R?

names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named.


1 Answers

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.)

like image 55
Jon Skeet Avatar answered Oct 11 '22 02:10

Jon Skeet