Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2006

Tip: Looking for answers? Try searching our database.

Printing the class that is running

Thread view: 
Roy Gourgi - 04 Nov 2006 18:45 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.