Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes how to obtain the name of an instance class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to obtain the name of an instance class" Watch "how to obtain the name of an instance class" New topic
Author

how to obtain the name of an instance class

Edwin Davidson
Greenhorn

Joined: Nov 26, 2003
Posts: 27
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

Joined: Oct 20, 2000
Posts: 84
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

Joined: Nov 26, 2003
Posts: 27
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

Joined: Nov 26, 2003
Posts: 27
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to obtain the name of an instance class
 
Similar Threads
Overloading constructors with differing intializations
Are my classes shaped correctly?
Animating with Swing Timer
Question On Comparator
Methods