I want to get the real value of a generic class member (an array) in Java. I have googled it for several hours but have not found a clear answer. Honestly, I'm new to Java and this generic stuff.
public class Box<T> {
private T t;
public Box(T t) { this.t = t; }
public void getTheT() {
// get the value of t at index 0
System.out.println(this.t[0]); // this doesn't work
}
}
public class App {
public static void main(String[] args) {
Integer[] Arr = {2,3,4};
Box<Integer[]> i = new Box<Integer[]>(Arr);
i.getTheT();
}
}
I've updated my question. Maybe this time it's clearer.
How can I get the original value of t
?
You should simply cast
t
to its original typeInteger[]
, like this :