Working with the class keyword in Java

后端 未结 8 1325
梦毁少年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:27

    Here ClientResponse.class is an instance of Class. In general Class object represents type of an object. When you create new instance:

    Object obj = new ClientResponse()
    

    you can retrieve the class (type) of that object by calling:

    obj.getClass()
    

    So, why would you pass Class objects around? It's less common, but one reason is to allow some method create arbitrary number of instances of a given class:

    ClientResponse resp = ClientResponse.newInstance();
    

提交回复
热议问题