Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get the object created name  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
simple question , I wander for debug/logger reasons
does java has the option to know what is the current object name ?
example :
myClass test = new myClass();

can test object know its name is "test" ?

like getClass knows the class type?

thanks.
 
Mamata Mohanta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class myClass
{
}
class Simple
{
public static void main(String[] args)
{
myClass test = new myClass();


System.out.println("The class of " + test +
" is " + test.getClass().getName());

}
}
you can get it like this.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Mamata shows you is not what you're looking for.

Objects do not have names, so it is not possible to get the "name" of an object.

What you think is the name of the object, is just the name of a reference variable that refers to the object. Note that there can be multiple variables that refer to the same object. What would be the "name of the object" in that case?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!