you could probably do it like this:
class.getMethod("toString").getDeclaringClass();
Here is an example:
class Test { public String toString(){ return "From test"; } } public static void main(String[] args) throws NoSuchMethodException, SecurityException { String resut = Test.class.getMethod("toString").getDeclaringClass().getName(); System.out.println(resut); }
Now run this with overriding toString, then comment it out and you will see the output and "Test", then "Object"
source share