0

I'm having one function which is inherited in other classes. What I would like in that function is to get current class name in which is running. For example:

public class A {
    public void foo() {
        System.out.println(this.class.getName()); // I know this syntax is incorrect
    }
}

public class B extends A {
}

I would like when I call instance of class A, for function foo() to print "A", and when running foo from instance of class B to write "B", without having to explicitly overload foo() function.

EDIT: As Arvind said it solution I was looking for was:

this.getClass().getName() 
nmiculinic
  • 2,224
  • 3
  • 24
  • 39

1 Answers1

0

System.out.println(this.getClass().getName());

Viraj
  • 5,083
  • 6
  • 35
  • 76