How do you get full class name (including package) as a string?

L

laredotornado

Hi,

I'm using Java 1.6. Right now I have this in my code ...

Class<?> c = Class.forName
("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");

in which the string
"myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
to the current class (this) and its package. What is another way of
writing that expression that does not rely on hard-coding the package
and class name?

Thanks, - Dave
 
L

Lothar Kimmeringer

laredotornado said:
Class<?> c = Class.forName
("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");

in which the string
"myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
to the current class (this) and its package. What is another way of
writing that expression that does not rely on hard-coding the package
and class name?

this.getClass().getName()


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
L

Lew

this.getClass().getName()

Don't you mean just 'getClass()', since the assignment is to a 'Class'
variable and not a 'String'?

This is useful for getting the current class, but useless if "that
expression" to which the OP refers is 'Class.forName()' and not
'Class<?> c'.

If the goal is to load and initialize a class as 'forName()' does,
then 'getClass()' will not be available, or else the class is already
loaded and initialized and there's no need. For this goal one pretty
much needs a class name, but it can be read from an external source
rather than hard coded.

If the goal is to get the current class's 'Class' instance, then
either 'getClass()', as you say, or
'SelectTourValidationErrorsTest.class' yields it. The latter is
necessary in a static context.
 
L

Lothar Kimmeringer

Lew said:
Don't you mean just 'getClass()', since the assignment is to a 'Class'
variable and not a 'String'?

I was just answering what was asked (in the subject).
This is useful for getting the current class, but useless if "that
expression" to which the OP refers is 'Class.forName()' and not
'Class<?> c'.

If the goal is to load and initialize a class as 'forName()' does,
then 'getClass()' will not be available, or else the class is already
loaded and initialized and there's no need.

Exactly what I was understanding when reading the OP:
If the goal is to get the current class's 'Class' instance, then
either 'getClass()', as you say, or
'SelectTourValidationErrorsTest.class' yields it. The latter is
necessary in a static context.

Combining above "as a string" in the subject and "this" in the
OP-body I read the question "how can I get the classname from
a class as string".

If I'm wrong "laredotornado" should be question again and be
more verbose.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
J

Jim Janney

laredotornado said:
Hi,

I'm using Java 1.6. Right now I have this in my code ...

Class<?> c = Class.forName
("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");

in which the string
"myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
to the current class (this) and its package. What is another way of
writing that expression that does not rely on hard-coding the package
and class name?

Thanks, - Dave

If what you want is a reference to the class object, you can use a
class literal:

import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;

Although I believe this compiles to a call to Class.forName, so
there's no runtime benefit.
 
L

Lew

Jim said:
If what you want is a reference to the class object, you can use a
class literal:

import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;

Although I believe this compiles to a call to Class.forName, so
there's no runtime benefit.

That cannot be true. 'Class.forName(String)' initializes the class,
reference to the 'class' literal must not.
 
J

Jim Janney

Lew said:
That cannot be true. 'Class.forName(String)' initializes the class,
reference to the 'class' literal must not.

You're right. Compiling with Java 6 I see a single ldc instruction.
 
L

laredotornado

laredotornadowrote:

Lothar Kimmeringer  wrote:

Don't you mean just 'getClass()', since the assignment is to a 'Class'
variable and not a 'String'?

This is useful for getting the current class, but useless if "that
expression" to which the OP refers is 'Class.forName()' and not
'Class<?> c'.

If the goal is to load and initialize a class as 'forName()' does,
then 'getClass()' will not be available, or else the class is already
loaded and initialized and there's no need.  For this goal one pretty
much needs a class name, but it can be read from an external source
rather than hard coded.

If the goal is to get the current class's 'Class' instance, then
either 'getClass()', as you say, or
'SelectTourValidationErrorsTest.class' yields it.  The latter is
necessary in a static context.

To give this thread some closure, Lothar correctly answered my
question, as it was literally asked. Lew gave an even more concise
answer and I did end up using

Class<?> c = this.getClass();

Just that easy! Thanks, - Dave
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top