Hi,
How can I print the class that is presently running?
For example, if my class that contains main() is called HelloWorld I want to
print out HelloWorld.
I would like to use System.out.println() to do it.
TIA
Roy
Eric Sosman - 04 Nov 2006 19:01 GMT
> Hi,
>
> How can I print the class that is presently running?
I'm guessing that you mean "the class containing the
method that is presently running."
> For example, if my class that contains main() is called HelloWorld I want to
> print out HelloWorld.
>
> I would like to use System.out.println() to do it.
Best I can think of is to throw an Exception, catch it,
and get the class name from the Exception's stack trace. Then
you can print it any way you like.
... but one has to ask: "Why?"

Signature
Eric Sosman
esosman@acm-dot-org.invalid
Daniel Dyer - 04 Nov 2006 20:55 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> I would like to use System.out.println() to do it.
1. You know this information at the time you write the code. So just
scroll up to the top of the source file, copy the class name and paste it
into your println() method call.
2. Unless what you mean is that you want to find out the concrete type of
the object that the method is invoked on (in case it has been
sub-classed)... in which case use this.getClass() (won't work for static
methods like main, but you can just use the above approach for these).
Dan.

Signature
Daniel Dyer
http://www.uncommons.org
Roy Gourgi - 04 Nov 2006 21:01 GMT
I am trying to follow how a program that is quite complex evolves.
Thanks
Roy
> Hi,
>
[quoted text clipped - 7 lines]
> TIA
> Roy
Thomas Schodt - 05 Nov 2006 09:38 GMT
> I am trying to follow how a program that is quite complex evolves.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StackTraceElement.html
StackTraceElement[] history = (new Throwable()).getStackTrace();
Oliver Wong - 06 Nov 2006 22:01 GMT
>I am trying to follow how a program that is quite complex evolves.
See
http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/Logger.html
- Oliver