Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > class.getName()

Reply
Thread Tools

class.getName()

 
 
Robert Mark Bram
Guest
Posts: n/a
 
      06-17-2004
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



 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      06-17-2004
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.clas s) );
}

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

Bye
Michael

 
Reply With Quote
 
 
 
 
Robert Mark Bram
Guest
Posts: n/a
 
      06-17-2004
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/...rview.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



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-17-2004
"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
 
Reply With Quote
 
Robert Mark Bram
Guest
Posts: n/a
 
      06-17-2004
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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-17-2004
On Wed, 16 Jun 2004 23:45:02 -0600, Chris Smith <(E-Mail Removed)>
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.
 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      06-17-2004

"Robert Mark Bram" <none> wrote in message
news:40d1337a$0$12961$(E-Mail Removed) u...
> 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.


>
>



 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      06-17-2004
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
 
Reply With Quote
 
Jeff Robertson
Guest
Posts: n/a
 
      06-18-2004
Roedy Green <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On Wed, 16 Jun 2004 23:45:02 -0600, Chris Smith <(E-Mail Removed)>
> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments