class.getName()

Discussion in 'Java' started by Robert Mark Bram, Jun 17, 2004.

  1. Hi All!

    If I have a class called Test, I can always do this to get the class's name
    from a static context:

    Test.class.getName()

    Question 1:
    I think "class" is a variable belonging to Test - but "class" doesn't appear
    in the Object class.. where is it defined as a variable?
    Or is it a language feature, like an operator of some kind?

    Question 2:
    Is there a line I could insert to reference the class's name without having
    to know its name (in a static context)? For example I can do this in a
    non-static context:
    this.getClass().getName();
    But I would like to do the same job from a static context.. the idea being
    so that I can write some code that outputs a class name and 'blindly' paste
    that code into several classes, without having to edit it..

    Thanks for any advice!

    Rob
    :)
     
    Robert Mark Bram, Jun 17, 2004
    #1
    1. Advertising

  2. Robert Mark Bram schrieb:

    > Hi All!
    >
    > If I have a class called Test, I can always do this to get the class's name
    > from a static context:
    >
    > Test.class.getName()
    >
    > Question 1:
    > I think "class" is a variable belonging to Test - but "class" doesn't appear
    > in the Object class.. where is it defined as a variable?
    > Or is it a language feature, like an operator of some kind?


    It's a language feature, the so called 'class literal' (JLS 15.8.2).

    >
    > Question 2:
    > Is there a line I could insert to reference the class's name without having
    > to know its name (in a static context)? For example I can do this in a
    > non-static context:
    > this.getClass().getName();
    > But I would like to do the same job from a static context.. the idea being
    > so that I can write some code that outputs a class name and 'blindly' paste
    > that code into several classes, without having to edit it..


    I don't understand what you want to do, but I think the keyword is
    'reflection' (see java.lang.Class and java.lang.reflect).

    E.g.

    public String createSimpleCode( Class c ) {
    return c.getName() + " o = new " + c.getName() + "();";
    }

    public void printSomeCode() {
    System.out.println(
    createSimpleCode(TheFirstClassToCreateCodeFor.class) );
    }

    public void createSomeOtherCode() {
    System.out.println(
    createSimpleCode(TheSecondClassToCreateCodeFor.class) );
    }

    Bye
    Michael
     
    Michael Rauscher, Jun 17, 2004
    #2
    1. Advertising

  3. Hi Michael!

    Thank you for your response!

    > > Or is it a language feature, like an operator of some kind?

    > It's a language feature, the so called 'class literal' (JLS 15.8.2).

    Thank you. :)

    > > Question 2:
    > > Is there a line I could insert to reference the class's name without

    having
    > > to know its name (in a static context)? For example I can do this in a
    > > non-static context:
    > > this.getClass().getName();
    > > But I would like to do the same job from a static context.. the idea

    being
    > > so that I can write some code that outputs a class name and 'blindly'

    paste
    > > that code into several classes, without having to edit it..

    >
    > I don't understand what you want to do,


    I should have been more specific.. what I wanted to do is have a line of
    code to create a logger for each class, but I didn't want to edit the line -
    just blindly paste it into each class as a static.. At the moment I do this:

    import java.util.logging.*;
    ....
    private static Logger theLogger =
    Logger.getLogger(Test.class.getName());

    But I was after a way to be even more lazy so that I never have to go and
    edit the line to replace "Test" with the actual class name that I am pasting
    the line into..

    > but I think the keyword is
    > 'reflection' (see java.lang.Class and java.lang.reflect).


    I was thinking about this too, or letting the logger guess (which it tries
    by default), but I read that sometimes reflection cannot get the class or
    method name:
    http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html#1.2
    JavaTM Logging Overview
    "The latest generation of virtual machines perform extensive optimizations
    when JITing and may entirely remove stack frames, making it impossible to
    reliably locate the calling class and method."

    Rob
    :)
     
    Robert Mark Bram, Jun 17, 2004
    #3
  4. Robert Mark Bram

    Chris Smith Guest

    "Robert Mark Bram" <none> wrote:
    > I should have been more specific.. what I wanted to do is have a line of
    > code to create a logger for each class, but I didn't want to edit the line -
    > just blindly paste it into each class as a static..


    No, there's no good way to do that. Technically speaking, I believe it
    could be accomplished with:

    Class c = Class.forName(
    new Throwable().getStackTrace()[0].getClassName());

    But that's *really* ugly, and perhaps worse than the problem you're
    trying to solve in the first place. Furthermore, the results of
    getStackTrace() aren't really well-defined enough to have confidence
    that this will work.

    In general, though, you can't do this easily because it's assumed that
    when writing a class, you know the class name. By contrast, getClass()
    is available on an instance because the actual class may be a subclass
    of the one you're writing; and thus the class isn't known at compile
    time.

    > "The latest generation of virtual machines perform extensive optimizations
    > when JITing and may entirely remove stack frames, making it impossible to
    > reliably locate the calling class and method."


    Note that this will also be a problem with the code I wrote above.
    However, there just isn't a compile-time way to get that information
    without using the class name in an expression.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jun 17, 2004
    #4
  5. Hi Chris,

    Thank you for the information - it is very helpful to me.

    > In general, though, you can't do this

    [find the enclosing class name from a static member]
    > easily because it's assumed that
    > when writing a class, you know the class name. By contrast, getClass()
    > is available on an instance because the actual class may be a subclass
    > of the one you're writing; and thus the class isn't known at compile
    > time.


    Excellent point. I was hankering for a some batch/shell scripting magic with
    %0 or $0.. but objects are a lot more compicated. :)

    In this case I am going to rely on more specific code - I am resigned to
    doing a bit more than copy and pasting in the logger line.. :)

    Rob
    :)
     
    Robert Mark Bram, Jun 17, 2004
    #5
  6. Robert Mark Bram

    Roedy Green Guest

    On Wed, 16 Jun 2004 23:45:02 -0600, Chris Smith <>
    wrote or quoted :

    > new Throwable().getStackTrace()[0].getClassName());


    could you not encapsulate that to hide the ugliness? You have to
    account for being one deeper in the stack.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 17, 2004
    #6
  7. Robert Mark Bram

    Andy Fish Guest

    "Robert Mark Bram" <none> wrote in message
    news:40d1337a$0$12961$...
    > Hi Chris,
    >
    > Thank you for the information - it is very helpful to me.
    >
    > > In general, though, you can't do this

    > [find the enclosing class name from a static member]
    > > easily because it's assumed that
    > > when writing a class, you know the class name. By contrast, getClass()
    > > is available on an instance because the actual class may be a subclass
    > > of the one you're writing; and thus the class isn't known at compile
    > > time.

    >


    I have also found this to be a pain, especially when instantiating loggers
    for log4j.

    it's here that I realised that the concept of 'static' methods in java is
    subtly different from 'class' or 'metaclass' concepts such as one might get
    in smalltalk.


    >
    >
     
    Andy Fish, Jun 17, 2004
    #7
  8. Robert Mark Bram

    Bryce Guest

    On Thu, 17 Jun 2004 10:03:44 +1000, "Robert Mark Bram" <none> wrote:

    >Hi All!
    >
    >If I have a class called Test, I can always do this to get the class's name
    >from a static context:
    >
    >Test.class.getName()
    >
    >Question 1:
    >I think "class" is a variable belonging to Test - but "class" doesn't appear
    >in the Object class.. where is it defined as a variable?
    >Or is it a language feature, like an operator of some kind?


    Because Class and Object are not the same. Look them up in any OO
    book.

    In summary, an Object is an instance of a class. So, to get the class
    name for an Object:

    this.getClass() will give you a Class object

    >Question 2:
    >Is there a line I could insert to reference the class's name without having
    >to know its name (in a static context)? For example I can do this in a
    >non-static context:
    >this.getClass().getName();
    >But I would like to do the same job from a static context.. the idea being
    >so that I can write some code that outputs a class name and 'blindly' paste
    >that code into several classes, without having to edit it..


    I guess I don't understand.

    You can always do: Class.forName(classNameAsString);

    for example
    Class clazz = Class.forName("mypackage.MyClass");

    then:
    clazz.getName();

    will return:
    mypackage.MyClass.

    The parameter to forName can be a variable, that is loaded from file,
    etc...

    --
    now with more cowbell
     
    Bryce, Jun 17, 2004
    #8
  9. Roedy Green <> wrote in message news:<>...
    > On Wed, 16 Jun 2004 23:45:02 -0600, Chris Smith <>
    > wrote or quoted :
    >
    > > new Throwable().getStackTrace()[0].getClassName());

    >
    > could you not encapsulate that to hide the ugliness? You have to
    > account for being one deeper in the stack.


    I'm pretty sure Log4J will do something like that if you configure it
    to do so. Logging is why the OP wanted this in the first place, right?
     
    Jeff Robertson, Jun 18, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    1
    Views:
    8,385
    Paul Tomblin
    Jun 27, 2003
  2. Rico
    Replies:
    4
    Views:
    423
    Chris Smith
    Dec 1, 2003
  3. Rico
    Replies:
    4
    Views:
    434
    Michael Borgwardt
    Dec 2, 2003
  4. Replies:
    1
    Views:
    476
    John C. Bollinger
    May 2, 2005
  5. WT
    Replies:
    6
    Views:
    950
    Walter Wang [MSFT]
    May 18, 2006
Loading...

Share This Page