Generic default constructor Java

public class Sample<T>{
public class Sample<T>{

 T data;

   Sample(){

     data = ????;

  }

}

How do I assign default values to data?

Solution

Bozho is right (you can't) If you absolutely want it to start with a value, use that value as an argument to the constructor For example:

public class Sample<T> {
  T data;
  Sample(T data) {
     this.data = data;
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>