inside static method - get name of class

S

Sean Kim

public abstract class Armor
{
// 1st way but not working
public static String getType()
{
return this.class.getSimpleName();
}
}

public class BackArmor extends Armor
{
// 2nd way
// I can do this but.. I wanna do 1st way
// public static String getType()
// {
// return BackArmor.class.getSimpleName();
// }
}

-------------------------

// I want to do like this
BackArmor.getType();

but 'this' is not working inside static method.
Is there any way I can get name of class in static method?


Thank you

Sean
 
L

Lew

Sean said:
public abstract class Armor
{
// 1st way but not working
public static String getType()
{
return this.class.getSimpleName();
}
}

Of course that doesn't work, since you cannot refer to non-static elements
within the static method. The method would have to return
'Armor.class.getSimpleName()'.
public class BackArmor extends Armor
{
// 2nd way
// I can do this but.. I wanna do 1st way
// public static String getType()
// {
// return BackArmor.class.getSimpleName();
// }
}

-------------------------

// I want to do like this
BackArmor.getType();

but 'this' is not working inside static method.
Is there any way I can get name of class in static method?

Why don't you want to return BackArmor.class.getSimpleName() from a static method?

Why must it be a static method?
 
L

Lew

Thomas said:
SecurityManager.getClassContext();

Since this is a protected, non-static method, you will need to instantiate a
SecurityManager subclass to get at the result.
<http://java.sun.com/javase/6/docs/api/java/lang/SecurityManager.html#getClassContext()>

This can be done but it's a lot harder than just using the 'class'
pseudo-variable. It's also semantically bizarre to instantiate a
SecurityManager when there's no security logic, just to get at class
information that's already provided for you.
 
E

Eric Sosman

Sean said:
public abstract class Armor
{
// 1st way but not working
public static String getType()
{
return this.class.getSimpleName();
}
}

public class BackArmor extends Armor
{
// 2nd way
// I can do this but.. I wanna do 1st way
// public static String getType()
// {
// return BackArmor.class.getSimpleName();
// }
}

-------------------------

// I want to do like this
BackArmor.getType();

but 'this' is not working inside static method.
Is there any way I can get name of class in static method?

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

Or, if you insist on the simple rather than fully-qualified name

try {
return Class.forName(new Throwable().getStackTrace()[0]
.getClassName()).getSimpleName();
} catch (ClassNotFoundException ex) {
// PANIC!
}
 
S

Stanimir Stamenkov

Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
return new Throwable().getStackTrace()[0].getClassName();

That should fail to identify subclasses and I speculate the OP tries
to achieve just that but I'm not really sure because of his approach
with static method(s).
 
E

Eric Sosman

Stanimir said:
Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
return new Throwable().getStackTrace()[0].getClassName();

That should fail to identify subclasses and I speculate the OP tries to
achieve just that but I'm not really sure because of his approach with
static method(s).

I'm not sure what you mean by "fail to identify subclasses."
Perhaps you meant nested classes? Even so, it will produce a
name like "org.borg.assimilate.Foo$Bar".

It's seems a rather dubious thing to want to do, and maybe
the O.P. should reexamine his reasons for wanting to do it.
Still, give him enough rope ...
 
D

Daniel Pitts

Eric said:
Stanimir said:
Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
return new Throwable().getStackTrace()[0].getClassName();

That should fail to identify subclasses and I speculate the OP tries
to achieve just that but I'm not really sure because of his approach
with static method(s).

I'm not sure what you mean by "fail to identify subclasses."
Perhaps you meant nested classes? Even so, it will produce a
name like "org.borg.assimilate.Foo$Bar".
Actually, what the O.P appears to be trying to do is to have an
inherited static method, and have that method display the class it was
called from. This is *not* possible in Java.
It's seems a rather dubious thing to want to do, and maybe
the O.P. should reexamine his reasons for wanting to do it.
Still, give him enough rope ...

My advice to the O.P.

Don't try to be clever with class names in this way, especially not with
static methods. You can make the classes implement an interface
(perhaps called Named).

What does this all buy you anyway? Why do SomeClass.getName() when you
can do "SomeClass" instead?
 
S

Stanimir Stamenkov

Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
Stanimir said:
Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
return new Throwable().getStackTrace()[0].getClassName();

That should fail to identify subclasses and I speculate the OP tries
to achieve just that but I'm not really sure because of his approach
with static method(s).

I'm not sure what you mean by "fail to identify subclasses."
Perhaps you meant nested classes? Even so, it will produce a
name like "org.borg.assimilate.Foo$Bar".

I've meant the case where the getType() is defined in a base class
but invoked from a subclass context which subclass doesn't redefine
the method:

public abstract class Armor {

public static String getType() {
return ...;
}
}

public class BackArmor extends Armor {

public void foo() {
...
String type = getType();
...
}

}

The same would happen if getType() is non-static and is not
overridden in the subclass.
 
L

Lew

Stanimir said:
Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:
Stanimir said:
Sun, 10 Feb 2008 17:35:24 -0500, /Eric Sosman/:

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

That should fail to identify subclasses and I speculate the OP tries
to achieve just that but I'm not really sure because of his approach
with static method(s).

I'm not sure what you mean by "fail to identify subclasses."
Perhaps you meant nested classes? Even so, it will produce a
name like "org.borg.assimilate.Foo$Bar".

I've meant the case where the getType() is defined in a base class but
invoked from a subclass context which subclass doesn't redefine the method:

public abstract class Armor {

public static String getType() {
return ...;
}
}

public class BackArmor extends Armor {

public void foo() {
...
String type = getType();
...
}

}

The same would happen if getType() is non-static and is not overridden
in the subclass.

You have shown why it's a bad idea to invoke static methods as if from a
subclass. Static methods belong to the class in which they're defined, not to
any subclass. Unless hidden, static methods are accessible from subclasses as
if they belonged to the subclass, but it's an illusion. Really they only
belong to the class in which they appear. Static methods cannot be
overridden, only hidden, by subclasses, so there's no workaround for subclass
invocation other than invoking them via the subclass.
 
K

Kevin McMurtrie

Sean Kim said:
public abstract class Armor
{
// 1st way but not working
public static String getType()
{
return this.class.getSimpleName();
}
}

public class BackArmor extends Armor
{
// 2nd way
// I can do this but.. I wanna do 1st way
// public static String getType()
// {
// return BackArmor.class.getSimpleName();
// }
}

-------------------------

// I want to do like this
BackArmor.getType();

but 'this' is not working inside static method.
Is there any way I can get name of class in static method?


Thank you

Sean

WTF?

Turn on compiler warnings and read about inheritance. The implementor
of static methods is always known at compile time.
 
S

Stefan Ram

Kevin McMurtrie said:
The implementor of static methods is always known at compile time.

I can understand that still sometimes one wants to avoid
source code redundancy:

If the class name appears several times within a source file
and one wants to change it, then one will need to change it
at several places - possibly missing a place.

Some IDEs might offer »refactors« to support this renaming,
but not everybody uses an IDE. (And those refactors might
still miss class names contained in string literals.)

(I use a customized preprocessor, which allows me to change
the package and the name of a Java class at a single point:
In the filename and the directory of the source file.
To change the package and the class name of a class, I only
need to move or rename its source file within the source
directory tree.

My preprocessor automatically adjusts the package and class
declaration within the source file and defines several macros
for use inside the file:

TYPENAME The name of the current class
HERE The name of the current package
THIS HERE.TYPENAME

So, to get the name of the »current class« as a string
literal, I can use »"TYPENAME"«.)
 
P

Patricia Shanahan

Kevin McMurtrie wrote:
....
Turn on compiler warnings and read about inheritance. The implementor
of static methods is always known at compile time.

But it is not necessarily known at coding time, which is when it would
need to be known in order to use the name in the source code.

There are at least two cases:

1. Refactoring, which Stefan Ram has already discussed.

2. Duplicated blocks of code. Suppose one has a static method that one
wishes to use in several classes, but that depends on the class or class
name. The method can be exactly duplicated if each copy can obtain the
name of its declaring class. If not, it has to be customized for each class.

I think it would have been better if Java had some syntax equivalent to
the instance method getClass() that could be used in a static context.
Providing that would not require any bytecode or JVM changes because, as
you say, the class name is known at compile time.

Patricia
 
K

Kevin McMurtrie

Patricia Shanahan said:
Kevin McMurtrie wrote:
...

But it is not necessarily known at coding time, which is when it would
need to be known in order to use the name in the source code.

There are at least two cases:

1. Refactoring, which Stefan Ram has already discussed.

I just read that. Had I known earlier that this was a workaround for a
broken refactoring hack, I wouldn't have bothered replying.
2. Duplicated blocks of code. Suppose one has a static method that one
wishes to use in several classes, but that depends on the class or class
name. The method can be exactly duplicated if each copy can obtain the
name of its declaring class. If not, it has to be customized for each class.

If the method is different for each class, then make a different method
for each class. Or stop using static methods.

Another way is for static methods to delegate to an instantiated class
referred to by a static field. It's a way to determine and set the
behavior of a static class at launch time.
 
R

Roedy Green

What are you talking about. What difference does it make to you how
someone posts to a newsgroup. Accusing Patricia of spamming is like
accusing the Queen of England of having atrocious table manners.
 
P

Patricia Shanahan

Roedy said:
What are you talking about. What difference does it make to you how
someone posts to a newsgroup. Accusing Patricia of spamming is like
accusing the Queen of England of having atrocious table manners.

I don't think Kevin's remark about Google spam was directed to me. He
seems to use it as part of his signature. As it happens, my article was
posted though the University of California at San Diego, but it would
have been neither a better nor a worse article if it had been posted
through Google.

Patricia
 
K

Kevin McMurtrie

Roedy Green said:
What are you talking about. What difference does it make to you how
someone posts to a newsgroup. Accusing Patricia of spamming is like
accusing the Queen of England of having atrocious table manners.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

It's a general statement about Google. Some newsgroups that I read are
40% to 70% Google spam, mostly from crime gangs. Google has made it
clear that they will not accept abuse complaints so the problem is only
getting worse. I've set my newsreader to discard all Google postings.
 
G

Gordon Beaton

Accusing Patricia of spamming is like accusing the Queen of England
of having atrocious table manners.

Who accused Patricia of spamming?

And if the Queen of England really does have atrocious table manners,
then she should be told to stop it and behave, just like anyone else
would. For someone whose only merit is that she was born into the
right family, she's got enough benefits as it is.

/gordon

--
 
L

Lew

Kevin McMurtrie said :
Kevin said:
It's a general statement about Google. Some newsgroups that I read are
40% to 70% Google spam, mostly from crime gangs. Google has made it
clear that they will not accept abuse complaints so the problem is only
getting worse. I've set my newsreader to discard all Google postings.

So stop using Google to read Usenet. As Patricia explained, /she/ didn't use
Google to post the message. In fact, your tag line doesn't make much sense
because the problem isn't with how people post - many folks do not use Google
to post to Usenet. If you're having trouble with spam from Google
specifically, it's not from how people are posting but from how you're reading.

There's also plenty of spam posted to Usenet that isn't posted via Google.
Blocking a message based on how it was posted is unlikely to do much beyond
blocking a lot of legitimate posts, while still permitting spam through.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top