Java Reflection question

  • Thread starter Albert Greinöcker
  • Start date
A

Albert Greinöcker

Hi NG,

maybe a too simple question: How can I get the type of a class within a
static method which belongs to the class for which I would like to have the
type?

thx,
Albert
 
C

Chris Uppal

Albert said:
maybe a too simple question: How can I get the type of a class within a
static method which belongs to the class for which I would like to have
the type?

If you mean that you want to avoid duplicating the class name in the code then
there isn't any straightforward way of doing so (one more flaw in the Java
language design). For instance in the following:

class MyClass
{
public static void
main(String[] args)
{
MyClass it = new MyClass();
it.doSomething();
}
...
}

there is no way to avoid the duplication of "MyClass" in the body of main() (or
rather, there is no way which isn't considerably more complicated than just
living with the problem).


If you mean that you want to get hold of the java.lang.Class object for the
class, then you can use the:

MyClass.class

syntax.

-- chris
 
P

Patricia Shanahan

Albert said:
Hi NG,

maybe a too simple question: How can I get the type of a class within a
static method which belongs to the class for which I would like to have the
type?

thx,
Albert

I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

public class NameTest {
public static void main(String[] args) {
Class myClass =
DummyForGettingClassName.class.getEnclosingClass();
System.out.println(myClass.getName());
}
private class DummyForGettingClassName{
}
}
 
P

Patricia Shanahan

Patricia said:
Albert said:
Hi NG,

maybe a too simple question: How can I get the type of a class within
a static method which belongs to the class for which I would like to
have the type?

thx,
Albert

I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

public class NameTest {
public static void main(String[] args) {
Class myClass =
DummyForGettingClassName.class.getEnclosingClass();
System.out.println(myClass.getName());
}
private class DummyForGettingClassName{
}
}

I should perhaps point out that I never use this. If there were a really
smooth way of avoiding repeating the class name I might use it. As it
is, I would write:

Class myClass = NameTest.class;

If the class name is long enough for a risk of typos, use copy-paste.
Never change a class name by editing it directly. Instead, use a
refactoring tool such as Eclipse to rename it.

Patricia
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

If we are in the department for hacks I would prefer:

String clsnam = (new Exception()).getStackTrace()[0].getClassName();

Arne
 
P

Patricia Shanahan

Arne said:
I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

If we are in the department for hacks I would prefer:

String clsnam = (new Exception()).getStackTrace()[0].getClassName();

Arne

I think I prefer that way if you only need the name, not the Class
object. I'm not sure which I prefer if you need the Class.

Patricia
 
J

Jeffrey Schwab

Arne said:
I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

If we are in the department for hacks I would prefer:

String clsnam = (new Exception()).getStackTrace()[0].getClassName();

I guess that's as reasonable as any other hack, but I'm still chuckling. :)
 
S

su_dang

Patricia said:
Arne said:
Patricia said:
Albert Greinöcker wrote:
maybe a too simple question: How can I get the type of a class within
a static method which belongs to the class for which I would like to
have the type?
I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:

If we are in the department for hacks I would prefer:

String clsnam = (new Exception()).getStackTrace()[0].getClassName();

Arne

I think I prefer that way if you only need the name, not the Class
object. I'm not sure which I prefer if you need the Class.

Patricia

If you have the name, may be you can use Class.forName to get the class
object.

One thing it concerns me is the javadocs says

Some virtual machines may, under some circumstances, omit one or more
stack frames from the stack trace. In the extreme case, a virtual
machine that has no stack trace information concerning this throwable
is permitted to return a zero-length array from this method. Generally
speaking, the array returned by this method will contain one element
for every frame that would be printed by printStackTrace

Try to get the first element of the array without checking its length
is a little bit dangerous.
 
T

Tor Iver Wilhelmsen

Albert Greinöcker said:
maybe a too simple question: How can I get the type of a class within a
static method which belongs to the class for which I would like to have the
type?

How likely are you to ever change the class' name? Use the
MyClass.class syntax; a refactoring tool should pick it up if you ever
change it anyway.
 

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

Similar Threads

Problem with reflection in C# 4
Trouble with reflection 0
Reflection 2
reflection 2
reflection 2
Java 0
Reflection and Annotation-arguments 4
Java matrix problem 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top