How to access a static method via a class reference
问题 class A { public static void foo() {} } class B { public static void foo() {} } I have Class clazz = A.class; or B.class ; How do I access this via "clazz" assuming it might be assigned either 'A' or 'B' 回答1: It is only possible to access those methods using reflection. You cannot reference a class directly, only an instance of type Class. To use reflection to invoke methodname(int a, String b): Method m = clazz.getMethod("methodname", Integer.class, String.class); m.invoke(null, 1, "Hello