-2

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?


java.lang.class - what I missed was I needed to append .class to Cls to get the type java.lang.class<Cls>

Sergey
  • 19,487
  • 13
  • 44
  • 68
  • You need to learn about reflection. – SLaks Aug 03 '16 at 18:18
  • We need more context (i.e. more of your code) to understand what concept you are having trouble with. Show us the definition of the function you want to call. – Jim Garrison Aug 03 '16 at 18:18
  • 1
    The former is an instance of `java.lang.Class`, the latter an instance of whatever class. You can append `.class` to a class name to get its class instance, e.g. `Class clazz = org.xbib.elasticsearch.action.termlist.TermlistRequest.class;`. – Siguza Aug 03 '16 at 18:21
  • @Siguza looks like I needed your comment – Sergey Aug 03 '16 at 18:23
  • 1
    To all "-": do I need to learn a language before asking questions about it? – Sergey Aug 03 '16 at 18:27
  • Before you asked the question did you search for `java.lang.Class` to see what it was? What did you find? What didn't you understand? – Sotirios Delimanolis Aug 03 '16 at 19:10
  • Nobody said any of that. You seem to be curious about the downvotes. When you hover over that button, you'll see that one of the reasons is the lack of research shown in a question. The questions in my previous comment are rhetorical. They are meant to have you edit that information into your question in order to improve it. – Sotirios Delimanolis Aug 03 '16 at 19:53

1 Answers1

3

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523