A Generic Class with Two Type Parameters : Generics Basics « Generics « Java Tutorial






class TwoGen<T, V> {
  T ob1;

  V ob2;

  TwoGen(T o1, V o2) {
    ob1 = o1;
    ob2 = o2;
  }

  void showTypes() {
    System.out.println("Type of T is " + ob1.getClass().getName());

    System.out.println("Type of V is " + ob2.getClass().getName());
  }

  T getob1() {
    return ob1;
  }

  V getob2() {
    return ob2;
  }
}

public class MainClass {
  public static void main(String args[]) {
    TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics");
    tgObj.showTypes();

    int v = tgObj.getob1();
    System.out.println("value: " + v);

    String str = tgObj.getob2();
    System.out.println("value: " + str);
  }
}
Type of T is java.lang.Integer
Type of V is java.lang.String
value: 88
value: Generics








12.1.Generics Basics
12.1.1.Life without Generics
12.1.2.What Are Generics? A Simple Generics Example
12.1.3.Generics Work Only with Objects
12.1.4.A Generic Class with Two Type Parameters
12.1.5.Introducing Generic Types
12.1.6.Working with generic List
12.1.7.Nested generic type
12.1.8.A generic type can accept more than one type variables.
12.1.9.Raw Types and Legacy Code