Sergey Sergey - 4 months ago 9
Java Question

What is the difference between java.lang.class<Cls> and just Cls?

What is the difference between

java.lang.class<Cls>
and just
Cls
?

I need to pass this to a function:

java.lang.Class<org.xbib.elasticsearch.action.termlist.TermlistRequest>


I tried
new TermlistRequest()
but it is this:

org.xbib.elasticsearch.action.termlist.TermlistRequest


Can you explain what is it in Java? And how to create an object of this type?

PS: I know it may be very simple, but I'm new to Java

Answer

The difference between Class<T> and T is that Class<T> represents metadata about T. You can look up T's methods and fields, and also create new instances by calling newInstance method.

As far as creating Class<T> objects goes, the simplest approach is to use MyClass.class syntax, e.g.

Class<TermlistRequest> tr = TermlistRequest.class;

Class (but not Class<T>) objects can also be obtained by resolving classes dynamically.

Comments