Working with the class keyword in Java

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

    A class is a "blueprint" of the object. The instance is a object.

    If we have

    public class SomeClass {
       int a;
       SomeClass(int a) {
          this.a = a
       }
    }
    

    We can have an instance of this class

    SomeClass c = new SomeClass(10);
    

    c is an instance of the class. It has a integer a with value 10.

    The object SomeClass.class represents a Class.

    Here SomeClass.class is a object of the type Class which has the information that SomeClass is

    1. a concrete class with
    2. one constructor
    3. with a integer member variable

      and lots more other metadata about the class SomeClass. Note that it does not have a value for a.

    You should use get(c) incase you are planning to do something with a instance of c like call c.a or other useful functions to manupulate/get data of the instance.

    You should use get(SomeClass.class) when the get returns something based on the fact that the argument is some type of class. For example, if this is a method on a Registry class which has a map which retrieves a implementation class based on type of class passed in.

    0 讨论(0)
  • 2021-02-13 19:27

    Here ClientResponse.class is an instance of Class<ClientResponse>. 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();
    
    0 讨论(0)
  • 2021-02-13 19:27

    Whenever we compile any Java file, the compiler will embed a public, static, final field named class, of the type java.lang.Class, in the emitted byte code. Since this field is public and static, we can access it using dotted notation along with class name as in your case it is ClientResponse.class.

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2021-02-13 19:35

    The very most important fact is - you don't need to have an instance to call the method. It's critically useful in situations when you cannot for some reason instantiate a class, e.g. it's abstract, or have only private constructor, or can only be correctly instantiated by some framework, like Spring or JSF.

    You can then call get to obtain an object of a requested type without even knowing where it does come from and how it get's created.

    0 讨论(0)
  • 2021-02-13 19:36

    There's a lot of ways Class objects can be used. This is used for Reflection. Below is a link that can help you understand more.

    http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

    0 讨论(0)
提交回复
热议问题