1

I wanted to obtain class object of Iterator and discovered that

 Iterator<String>.class 

is not valid. This highlighted a question if its possible at all to use

 ".class"

syntax for

Object<T> 

kind of objects in Java.

If yes, then how it can be done? If not, what are the alternatives ?

I am doing Mocking for Iterator using Mockito's

Mockito.mock(Class<T>)

syntax.

DolphinJava
  • 2,682
  • 1
  • 23
  • 37
  • That is very complicated way of asking the same question. People with this problem may find it difficult to find that question. – DolphinJava Sep 05 '14 at 19:21

2 Answers2

0

You can't do this because generics in Java are implemented using erasure i.e. you don't get specialized version of your classes for each type parameter used at runtime. Take a look at this answer in case it didn't turn up in your search.

Community
  • 1
  • 1
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
0

It is not possible. The way that generics are implemented in Java, they only exist at compile time, for the purpose of type checking.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80