0

In a method, I would like to get the classname for a class which is extended by another. But, if I call this method on the child, I have this child's name.

How can I refer to the class where the method is written? Not to the extended class.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
Florent06
  • 1,008
  • 2
  • 10
  • 29
  • this is not what I want to do. In the parent class, I want to get the classname of THIS class. But when I do this.getClass() and I extend this class, it refers to the child class. – Florent06 Jul 23 '13 at 08:55
  • Don't edit your questions to include things like "Resolved" in the name as such erroneous forum conventions are not useful on SO. – Tim Bender Jul 23 '13 at 09:10

2 Answers2

4

When you have a class like

public class A {
}

You can reference the defining class by using A (the class name). E.g.

public void do() {
    System.out.println("Defined in " + A.class.getName());
}
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
2

If i understand correctly, you want to get the super-class's name.

using Reflection API:

  Class subclass = o.getClass();
  Class superclass = subclass.getSuperclass();
  String className = superclass.getName();
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • this is not what I want to do. In the parent class, I want to get the classname of THIS class. But when I do this.getClass() and I extend this class, it refers to the child class. – Florent06 Jul 23 '13 at 08:58