0

Getting error with genric for define genric type variable in java

Define variable with genric type

Example with generic, normal, main class

//Generic Class
public class A <T> {
    T t;

    //getter setter
}

//Normal class
public class B {
    //Some variables
}

//Main Class
public class C {
    public static void main(String[] args) {
        //Fine with normal class
        doSomeOpration(B.class);

        //Call doSomeOpration with genric class
        doSomeOpration(A<B>.class); //getting error not resolved A<B> generic type
    }

    public <T> static void doSomeOpration(Class<T> clazz) {
        //Do Some opration with clazz
    }
}
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
Ankit Agarwal
  • 166
  • 1
  • 7
  • 2
    When you were asking your question, there was a big orange **How to Format** box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a **[?]** button giving formatting help. *And* a preview area located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Jul 21 '16 at 12:17
  • 1
    I've fixed the code formatting and indentation, and some spelling, for you. (In particular: It's "generic", not "genric".) – T.J. Crowder Jul 21 '16 at 12:20
  • it is impossible because of erasure. – Mykola Yashchenko Jul 21 '16 at 12:24

1 Answers1

0

I suggest you take a look at the following post.

What is the concept of erasure in generics in Java?

TL;DR: You cannot use T.class or A.class due to type erasure.

Community
  • 1
  • 1
Lucas Ces
  • 86
  • 5