Getclass () vs .class

Possible duplicate:
What is the difference between calling MyClass.class and MyClass.getClass ()

Desire to have a Class<T> object, what is the difference between these two approaches?

 Test ob = new Test(); ob.getclass(); 

or

 Test.class 
+4
source share
2 answers

By Class javadoc

The first approach is to get a Class object from Object .

the second approach gets the Class object for the named type (or for void) using the class literal.

+3
source

The getClass() method is defined in java.lang.Object and, therefore, can be called by any reference to the object.

It gets the class object associated with the runtime type of the object to which the reference points belong.,

The getClass () method is actually not like .class. Closer is a Class.forName (String) that receives a class object for a class called String.

In situations where they can be used, use .class as it is more efficient.

+4
source

Source: https://habr.com/ru/post/1445840/


All Articles