Passing a class (“Country.class”) as an argument in Java

后端 未结 7 1090
感情败类
感情败类 2020-12-12 06:29

I\'m trying to make a method that takes an argument of Country.class, User.class etc, and returns argument.count().

All the po

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 07:14

    In reply to your comment to ZZ Coder; a static method in Java is called in the namespace context of a class, like Model.count() for a static method count() in the class Model, but the method does not become part of Model.class, Model.class is an instance of Class describing the class Model. (I can see where the confusion originates, it would be logical to have a specialised Model.class that includes the static methods, but Java isn't desinged that way.)

    Your way out is to use reflection to call the static count() for the class that you pass to your code.

提交回复
热议问题