Source of BoxGeneric.java


  1: //BoxGeneric.java
  2: //A simple generic version of a Box class.
  3: //Here, T is a type parameter that will be replaced by an
  4: //actual type when an object of type BoxGeneric is created.
  5: 
  6: public class BoxGeneric<T>
  7: {
  8:     private T t;
  9: 
 10:     public BoxGeneric(T t)
 11:     {
 12:         this.t = t;
 13:     }
 14: 
 15:     public void setData(T t)
 16:     {
 17:         this.t = t;
 18:     }
 19: 
 20:     public T getData()
 21:     {
 22:         return t;
 23:     }
 24: 
 25:     //Show the type of the private instance variable t.
 26:     public void showType()
 27:     {
 28:         System.out.println(
 29:             "The type of t is " + t.getClass().getName() + "."
 30:         );
 31:     }
 32: }