-1

I want to create class reference for Generic class .

I am having a generic class called GenericRtt coded as below:

package com.practice.rtt;

public class GenericRtt<T> {

    private T s;


    public T getS() {
        return s;
    }


    public void setS(T s) {
        this.s = s;
    }

}

I want to create reference not instance of the class as .
Class <GenericRtt<String>> generics=(Class<GenericRtt<String>>) GenericRtt.class;
I don't know how to create class reference of the GenericRtt.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
shukla
  • 57
  • 6

3 Answers3

0

You can't. The type of Class can only be the raw type.

The closest you can get is:

Class <GenericRtt> clazz = GenericRtt.class;
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can't. You should read about type erasure in Java

also this might be very helpful java-generic-class-determine-type

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
0

NO.

Even you have a reference ,Ultimately you have to create a object to use the reference.

GenericRtt rt = new GenericRtt();

Then you can assign it or refer to some declared reference ,I guess.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307