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 / March 2004

Tip: Looking for answers? Try searching our database.

Getting the current class name in static  code

Thread view: 
Stephen Chell - 01 Mar 2004 02:09 GMT
Is there a way to get the name of the current class in static code
without actually naming the class in your code?

Background: notice the static declaration in the following code.  In
order to pass the name of the current glass (com.foo.Bar) to the
Logger.getLogger() method, I have to specifically name the Bar class
in the method call.

package com.foo;
import org.apache.log4j.Logger;

public class Bar {
 static Logger logger = Logger.getLogger(Bar.class);
 
 ...
}

If that declaration is copied and pasted into many class files, I have
to manually amend it each time to pass the correct class name as a
parameter.  It would be much nicer if there was a generic way to pass
the current class name, something like the following:

package com.foo;
import org.apache.log4j.Logger;

public class Bar {
 static Logger logger = Logger.getLogger(This);
 
 ...
}

... but of course that doesn't work (at least not in JDK 1.4.2.)  Are
there any reasonable alternatives?

Cheers
Steve
Andrew Hobbs - 01 Mar 2004 06:38 GMT
> Is there a way to get the name of the current class in static code
> without actually naming the class in your code?
[quoted text clipped - 4 lines]
> ... but of course that doesn't work (at least not in JDK 1.4.2.)  Are
> there any reasonable alternatives?

Have you had a look at the docs for java.lang.Class.  It has your answer
there.

Cheers

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************

> Cheers
> Steve
S Manohar - 01 Mar 2004 14:02 GMT
> > Is there a way to get the name of the current class in static code
> > without actually naming the class in your code?
[quoted text clipped - 5 lines]
>
> Andrew

:( Looked there but can't find answer - is there a way of doing this?
Andrew Hobbs - 02 Mar 2004 01:04 GMT
> > > Is there a way to get the name of the current class in static code
> > > without actually naming the class in your code?
[quoted text clipped - 7 lines]
>
> :( Looked there but can't find answer - is there a way of doing this?

Sorry I misread your original post.  You wanted it as a static method.
However what I would like to know is in what circumstances could you
possibly use it as a static method.  ie in your original post you mentioned
the "current class".  What do you mean by a current class.  When do you ever
refer to a current class. (and not as a current object).

In your original post you posted some code that went

"If that declaration is copied and pasted into many class files, I have
to manually amend it each time to pass the correct class name as a
parameter."

Obviously you seem to have it working but I still fail to see how you could
be using it as a static method?

You then said
"It would be much nicer if there was a generic way to pass
the current class name, something like the following:

package com.foo;
import org.apache.log4j.Logger;

public class Bar {
 static Logger logger = Logger.getLogger(This);"

However the word "this" has no meaning in a static context.  "this" only has
meaning in terms of an instance of a class.

Cheers

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Bent C Dalager - 02 Mar 2004 19:08 GMT
>However the word "this" has no meaning in a static context.  "this" only has
>meaning in terms of an instance of a class.

Note that he wrote "This", not "this".

"This" is apparantly a hypothetical keyword (presumably evaluating to
an instance of type Class) which, in a non-static method, would make
the following statement true:

this.getClass().equals(This);

The keyword "This" would be useful in that in a static method, you
could write something like

static void printClassName()
{
  System.out.println("This class is " + This.getName());
}

As far as I can tell, it would be useful mostly in connection with
logging and debug output and, in particular, in boilerplate code that
you just want to paste into a hundred classes and have it auto-work
without having to substitute parts of the boilerplate with the real
name of each class you're pasting it into.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Stephen Chell - 02 Mar 2004 01:39 GMT
> > Is there a way to get the name of the current class in static code
> > without actually naming the class in your code?
[quoted text clipped - 11 lines]
>
> Andrew

I have checked java.lang.Class, but I don't see any answer to my
question.
Getting the current class name within instance code is easy:

String className = this.getClass().getName()

But how do you do it in *static* code?   One possible way would be to
create an exception and the class name out of the stack trace, but
that would be a relatively expensive and inelegant solution.

Cheers
Steve
Andrew Hobbs - 02 Mar 2004 01:55 GMT
.......

> But how do you do it in *static* code?   One possible way would be to
> create an exception and the class name out of the stack trace, but
> that would be a relatively expensive and inelegant solution.

How are you going to create an exception if you don't know the name of the
class to call the static method ?  In other words, what do you mean by
'current class'?

> Cheers
> Steve
Mark Haase - 02 Mar 2004 05:24 GMT
> How are you going to create an exception if you don't know the name of the
> class to call the static method ?  In other words, what do you mean by
> 'current class'?

The class which contains the top-most method which the thread of
execution is currently in?

|\/|  /|  |2  |<
mehaase(at)sas(dot)upenn(dot)edu
Oscar kind - 01 Mar 2004 11:17 GMT
> Is there a way to get the name of the current class in static code
> without actually naming the class in your code?

If you mean that a piece of static code defined in a superclass will be
able to determine the current subclass, then no.

The reason for this is, that static code exists in the superclass only. It
is not inherited.

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Java/J2EE Developer                             email available on website

Stephen Chell - 02 Mar 2004 01:42 GMT
> > Is there a way to get the name of the current class in static code
> > without actually naming the class in your code?
>
> If you mean that a piece of static code defined in a superclass will be
> able to determine the current subclass, then no.

No, I don't mean that.  I want the static code to get the name of the local class.  

> The reason for this is, that static code exists in the superclass only. It
> is not inherited.
>
> Oscar
Thomas Schodt - 01 Mar 2004 14:25 GMT
> Is there a way to get the name of the current class in static code
> without actually naming the class in your code?

Like this? (Java 1.4)

class Klass {
    public static void main(String[] arg) {
        Throwable ex = new Throwable();
        StackTraceElement[] va = ex.getStackTrace();
        System.out.println(va[0].getClassName());
    }
}
Stephen Chell - 02 Mar 2004 01:54 GMT
> > Is there a way to get the name of the current class in static code
> > without actually naming the class in your code?
[quoted text clipped - 8 lines]
>     }
> }

I guess that would work.  Seems a little expensive though.  Java
really needs a static equivalent of "this".

Cheers
Steve
Tor Iver Wilhelmsen - 02 Mar 2004 18:12 GMT
> I guess that would work.  Seems a little expensive though.  Java
> really needs a static equivalent of "this".

No, because one is assumed to be able to read the name in the source
one is writing in and if necessary copy and paste it as needed.
Darryl L. Pierce,,, - 02 Mar 2004 18:27 GMT
> I guess that would work.  Seems a little expensive though.  Java
> really needs a static equivalent of "this".

And what would that reference point to?

Signature

Darryl L. Pierce <mcpierce@myrealbox.com>
Visit the Infobahn Offramp - <http://mypage.org/mcpierce>
"What do you care what other people think, Mr. Feynman?"



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.