Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between MyClass.class and Class.forName("className")

We can get class Class object by 3 methods:

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

I don't understood the difference between: MyClass.class and Class.forName("className").

Because both will need Class Name.

like image 617
Neal Avatar asked Jun 18 '12 04:06

Neal


1 Answers

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

like image 100
swapy Avatar answered Oct 16 '22 17:10

swapy