class.getName()

R

Robert Mark Bram

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
:)
 
M

Michael Rauscher

Robert said:
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
 
R

Robert Mark Bram

Hi Michael!

Thank you for your response!
It's a language feature, the so called 'class literal' (JLS 15.8.2).
Thank you. :)
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
:)
 
C

Chris Smith

Robert Mark Bram said:
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
 
R

Robert Mark Bram

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
:)
 
R

Roedy Green

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.
 
A

Andy Fish

Robert Mark Bram said:
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.
 
B

Bryce

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...
 
J

Jeff Robertson

Roedy Green said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top