What does .class mean after a class name

前端 未结 3 1336
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 12:03

What does .class mean in, for example, MyClass.class? I understand that when you put the name of a class and the point it is used to access its sta

3条回答
  •  别那么骄傲
    2020-12-25 12:52

    When you write .class after a class name, it references the Class object that represents the given class (formally, it is a named class literal). The the type of NombreClase.class is Class.

    E.g., NombreClase.class is an object that represents the class NombreClase on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of NombreClase.

    NombreClase obj = new NombreClase();
    System.out.println(NombreClase.class.getName());
    System.out.println(obj.getClass().getName())
    

提交回复
热议问题