Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I print the variable name holding my object?

For example, I have:

myclass ob=new myclass()

How would I print "ob"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...