Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to obtain the name of an instance class

 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class x
{
public void printline()
{
System.out.println("am in class x"):
System.out.println("instance is newx"):
}
}
***************************************************
In the Main() procedure it says:
x newx = new x();
newx.printline();
newx.printline will spit out 2 lines to the screen. But I want the 2nd line to say the name of the instance i.e. "instance is newx" without my hard coding it like above.
There is a method that will return the name of the instance that calls x, isn't there ? It's something like getNmae() or something of that flavor I think.
Can one of you kind folks clue me in please ?
Thanks.
Edwin
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the name of the class:
System.out.println( getClass().getName() );
getClass() returns the runtime class of the object, then you can use getName() to return the name of the class. getClass() is a method of Object, getName() is a method of Class.
[ March 20, 2004: Message edited by: Carlos Failde ]
 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my actual code:
*****************************************
class one
{
public void runthis()
{
System.out.println(getClass());
System.out.println(getClass().getName());
}
}
class namee
{
public static void main(String x[])
{
one edwin=new one();
edwin.runthis();
}
}
*************************************************
what I want is to have edwin.runthis() say "edwin" and not "one". The above 2 runthis() code lines says "class one" and "one", respectfully. But the instance calling runthis() is called "edwin" and I want runthis() to say the name of the instance that is calling it, if you will.
Thanks.
Edwin
 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my actual code:
*****************************************
class one
{
public void runthis()
{
System.out.println(getClass());
System.out.println(getClass().getName());
}
}
class namee
{
public static void main(String x[])
{
one edwin=new one();
edwin.runthis();
}
}
*************************************************
what I want is to have edwin.runthis() say "edwin" and not "one". The above 2 runthis() code lines says "class one" and "one", respectfully. But the instance calling runthis() is called "edwin" and I want runthis() to say the name of the instance that is calling it, if you will.
Thanks.
Edwin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're out of luck. Edwin is the name of a variable that references the object. That's not really available to us. The fact is, there could be many variables all referencing the same object. How would you ever choose which name to use?

Were you trying to solve a problem that looked like this would work? If so, maybe we can help find another way. If you were just trying to enhance your debugging statements, I think we hit a dead end.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic