public class Sample<T>{
T data;
Sample(){
data = ????;
}
}
How can i assign a default value to data ?
Bozho is right (you can't). If you definitely want it to start off with a value, make that value an argument to the constructor. For instance:
public class Sample<T> {
T data;
Sample(T data) {
this.data = data;
}
}
You can't. The type T
is erased at runtime, so you can't instantiate it.
If you pass a Class
argument to the Sample(..)
constructor, you can call clazz.newInstance()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With