Re: How to get the runtime class?



Robert wrote:
I think it'll work in any method that the child would call inside the
parent.  Just probably not the ctor.  Try any other method define the
method in the abstract class and have the derived class call it but
don't define it in the derived class.  I bet it works.


Why would the constructor be any different?

public abstract class ClassTest {
  String myClassName1 = getClass().getName();
  String myClassName2;
  ClassTest(){
    myClassName2 = getClass().getName();
  }
  public static void main(String[] args) {
    ClassTest t = new foo2();
    System.out.println(t.getClassName());
    System.out.println(t.myClassName1);
    System.out.println(t.myClassName2);
  }
  String getClassName(){
    return getClass().getName();
  }
}

class foo2 extends ClassTest{
}


prints:

foo2
foo2
foo2

.