Working with the class keyword in Java

后端 未结 8 1346
梦毁少年i
梦毁少年i 2021-02-13 19:16

I don\'t really understand how the class keywords work in some instances.

For example, the get(ClientResponse.class) method takes the Cl

8条回答
  •  自闭症患者
    2021-02-13 19:30

    The Class class, which is different from the class keyword, is meta-data describing instances. It tells you about the methods, data members, constructors, and other features of the instances that you create by calling new.

    For example get(ClientResponse.class) method takes the ClientResponse.class how does it uses this when it gets it and what are the advantage over just passing a instance of it?

    You can't pass an instance of ClientResponse to this method; it's expecting meta-data about all instances of ClientResponse. If you passed an instance, you'd expect that the method might change the state of that instance. But passing the meta-data about all instances might allow the method to create a new kind of instance (e.g. a dynamic proxy) or do something else that depends on the meta-data about all instances of ClientResponse. See the difference?

提交回复
热议问题