How to access a static method via a class reference

会有一股神秘感。 提交于 2020-04-08 08:55:31

问题


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 World!");

See Class.getMethod() and Method.invoke()

You may want to think about your design again, to avoid the need to dynamically call static methods.




回答2:


You can invoke a static method via reflection like this :

Method method = clazz.getMethod("methodname", argstype);
Object o = method.invoke(null, args);

Where argstype is an array of arguments type and args is an array of parameters for the call. More informations on the following links :

  • getMethod()
  • invoke()

In your case, something like this should work :

Method method = clazz.getMethod("foo", null);
method.invoke(null, null); // foo returns nothing



回答3:


You cannot access static methods without an explicit reference to the class.

No inheritance here, sorry, so you must either do:

A.foo()

or

B.foo()

If you really need it, you will have to do a check:

Object o = .... // eith an A or B instance.
if( o instanceof A ) {
    A.foo()
} else {
    B.foo()
}

But why don't you just make those functions instance functions, and let them implement an interface?

Okey, you have a class object. Then do:

Class c = ...;
c.getMethod("foo").invoke(null); // null to invoke static methods



回答4:


According to my lack of knowledge the need for the requested construct is given by the fact that an interface doesn't offer the possibility of static abstract methods. Here is an example:

public enum Cheese implements Yumy {
  GOUDA(49),
  ESROM(40),
  HWARTI(38);
  private int percentage;
  private Cheese(int fat100) {...} constructor
  public void yamyam() {...} // as in Yumy
  public static Cheese getByFat(int fat100) {...} // no chance to be part
                                                     of interface
};



回答5:


I hope this isn't making too many assumptions or deviating too far from your question, but if your two classes share a common supertype and creating an instance is tolerable then you can:

  1. Implement a common interface
  2. Create an instance of the object via myClass.newInstance() (class must have an empty constructor)
  3. Call the static method from the instance object.


interface Foo {
    void foo();
}
class A implements Foo {...}
class B implements Foo {...}

<T extends Foo> public void something(Class<T> clazz) {
    T myInstance = clazz.newInstance();
    myInstance.foo();
}

...
something(A.class);

It's a little bizarre but in my case it proved to be useful, and I began by asking the very same question that you did.



来源:https://stackoverflow.com/questions/5203024/how-to-access-a-static-method-via-a-class-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!