How to do `MyClass.class` in Java?

前端 未结 6 1275
有刺的猬
有刺的猬 2020-12-15 21:46

How can call public T doit(Class clazz); using MyClass.class as clazz where I can not instantiate or extend MyClas

6条回答
  •  抹茶落季
    2020-12-15 22:26

    Since after your update your question does not appear to be an exact duplicate:

    You would need to call getClass() on an instance of MyClass. Better have a dummy static final instance somewhere:

    public static final MyClass INSTANCE = new MyClass();
    ...
    return (Class>) instance.getClass();
    

提交回复
热议问题