Generic Constructors : Generic Method « Generics « Java Tutorial






class GenericClass {
  private double val;

  <T extends Number> GenericClass(T arg) {
    val = arg.doubleValue();
  }

  void showValue() {
    System.out.println("val: " + val);
  }
}

public class MainClass {
  public static void main(String args[]) {

    GenericClass test = new GenericClass(100);
    GenericClass test2 = new GenericClass(123.5F);

    test.showValue();
    test2.showValue();
  }
}
val: 100.0
val: 123.5








12.3.Generic Method
12.3.1.Creating a Generic Method
12.3.2.Generic Constructors
12.3.3.Generic methods: Max. Min
12.3.4.Generic method maximum returns the largest of three objects
12.3.5.Using generic methods to print array of different types
12.3.6.Wildcard test program