Re: How to get the runtime class?



Fredy wrote:

I have 2 classes

abstract public class foo {


public void bar() {
// i want a Class object of "this"


Class c = this.getClass(); // here i get a class of type foo, even if the runtime class is type foo2
}
}


public class foo2 extends foo {

....
}

with other words, how to get the children object from the parent object?

I think you may have a problem in your testing.

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

class foo2 extends ClassTest{
}


prints "foo2".

Patricia

.