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

前端 未结 4 446
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 07:53

    Class.forName("className"); 
    

    forName is a static method of class "Class". we require to provide the fully qualified name of the desired class. this can be used when name of class will come to known at runtime.

    ClassName.class;
    

    .class is not a method it is a keyword and can be used with primitive type like int. when Name of Class is known in advance & it is added to project, that time we use ClassName.class

提交回复
热议问题