Let\'s say I have a base class named Entity
. In that class, I have a static method to retrieve the class name:
class Entity {
public static
it is very simple done by
User.getClass().getSuperclass()
Your question is ambiguous but as far as I can tell you want to know the current class from a static method. The fact that classes inherit from each other is irrelevant but for the sake of the discussion I implemented it this way as well.
class Parent { public static void printClass() { System.out.println(Thread.currentThread().getStackTrace()[2].getClassName()); } } public class Test extends Parent { public static void main(String[] args) { printClass(); } }
Don't make the method static. The issue is that when you invoke getClass()
you are calling the method in the super class - static methods are not inherited. In addition, you are basically name-shadowing Object.getClass()
, which is confusing.
If you need to log the classname within the superclass, use
return this.getClass().getName();
This will return "Entity" when you have an Entity
instance, "User" when you have a User
instance, etc.
Each time the extended class is instantiated, its name will be stored in the String and accessible with the getter.
Why do you want to implement your own getClass() method? You can just use
System.out.println(User.class);
Edit (to elaborate a bit): You want the method to be static. In that case you must call the method on the class whose class name you want, be it the sub-class or the super-class. Then instead of calling MyClass.getClass()
, you can just call MyClass.class
or MyClass.class.getName()
.
Also, you are creating a static
method with the same signature as the Object.getClass()
instance method, which won't compile.
The superclass should not even know of the existence of the subclass, much less perform operations based on the fully qualified name of the subclass. If you do need operations based on what the exact class is, and can't perform the necessary function by inheritance, you should do something along these lines:
public class MyClassUtil
{
public static String doWorkBasedOnClass(Class<?> clazz)
{
if(clazz == MyNormalClass.class)
{
// Stuff with MyNormalClass
// Will not work for subclasses of MyNormalClass
}
if(isSubclassOf(clazz, MyNormalSuperclass.class))
{
// Stuff with MyNormalSuperclass or any subclasses
}
// Similar code for interface implementations
}
private static boolean isSubclassOf(Class<?> subclass, Class<?> superclass)
{
if(subclass == superclass || superclass == Object.class) return true;
while(subclass != superclass && subclass != Object.class)
{
subclass = subclass.getSuperclass();
}
return false;
}
}
(Untested code)
This class doesn't know about its own subclasses, either, but rather uses the Class
class to perform operations. Most likely, it'll still be tightly linked with implementations (generally a bad thing, or if not bad it's not especially good), but I think a structure like this is better than a superclass figuring out what all of its subclasses are.