Difference between MyClass.class and Class.forName(“className”)

前端 未结 4 431
Happy的楠姐
Happy的楠姐 2021-01-05 07:25

We can get class Class object by 3 methods:

  • MyClass.class
  • obj.getClass
  • Class.forName(\"className\")

I don\'t understood the di

4条回答
  •  不思量自难忘°
    2021-01-05 08:14

    Class.forName("className"); 
    

    It dynamically load the class based on fully qualified class name string.

    obj.getClass
    

    Returns the java.lang.Class object that represents the runtime class of the object.

    MyClass.class:
    

    A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a'.' and the token class. The type of C.class, where C is the name of a class, interface, or array type is Class.

    http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题