Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Can a parent class statically retrieve the class name of a child class?

In reference to Java, I would like to statically know the class name of the current class. A is the parent class of B. I would like to have a static String in A (parent class) which contains the class name of the current class, but when this static String is referenced in B (child class), it should contain the class name of B. Is this possible?

Example:

public class Parent {

protected static String MY_CLASS_NAME = ???
.
.
.
}

public class Child extends Parent {

public void testMethod() {
     if (MY_CLASS_NAME.equals(getClass().getName())) {
        System.out.println("We're equal!");
     }
}

}
like image 676
ChaimKut Avatar asked Jan 16 '11 13:01

ChaimKut


People also ask

Can parent and child classes have same data member in Java?

Parent and Child classes having same data member in Java Java 8 Server Side Programming Programming The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable.

Can a parent class reference a child class object in Java?

If a parent reference variable is holding the reference of the child class and we have the “value” variable in both the parent and child class, it will refer to the parent class “value” variable, whether it is holding child class object reference.

What is the reference variable of the parent class?

The reference variable of the Parent class is capable to hold its object reference as well as its child object reference. In Java, methods are virtual by default (See this for details).

How to retrieve a class name in Java?

Retrieving a Class Name in Java 1 Overview. In this tutorial, we'll learn about four ways to retrieve a class's name from methods on the Class API: getSimpleName (), getName (), getTypeName () and getCanonicalName (). 2 Retrieving Simple Name. Let's begin with the getSimpleName () method. ... 3 Retrieving Other Names. ... 4 Conclusion. ...


2 Answers

The only way I know is the following: create protected constructor that accepts String in parent class.

class Parent {
    private final String className;
    protected Parent(String className) {
         this.className = className;
    }
}

public class Child extends Parent {
    public Child() {
        super("Child");
    }
}

BTW you can even improve this using new Throwable().getStackTrace() in paren's custructor. In this case you even do not have to enforce all children to pass their name to parent.

class Parent {
    private final String className;
    protected Parent() {
         StackTraceElement[] trace = new Throwable().getStackTrace();
         this.className = trace[1].getClassName();
    }
}
like image 61
AlexR Avatar answered Oct 25 '22 23:10

AlexR


No, that's not possible. There's only one copy of the static String (per ClassLoader), but you can have multiple subclasses.

You can however have a static field per (sub-)class, and then use a method

public class Child extends Parent {
   private static final String NAME = "some alias";

   @Override
   public String getName() {
       return NAME;
   }
}

This is a technique that can be used to avoid Reflection (the NAME then often doesn't equal the class name, but uses some alias - it can also be used with enums instead of Strings).

like image 36
Chris Lercher Avatar answered Oct 25 '22 21:10

Chris Lercher