Java examples for Object Oriented Design:Generic Class
Declaring a Generic Class Wrapper<T>
public class Main { public static void main(String[] args) { Wrapper<String> w1 = new Wrapper<String>("Hello"); String s1 = w1.get();// w w w .j a va 2 s .c om System.out.println("s1=" + s1); w1.set("Testing generics"); String s2 = w1.get(); System.out.println("s2=" + s2); w1.set(null); String s3 = w1.get(); System.out.println("s3=" + s3); } } class Wrapper<T> { private T obj; public Wrapper(T obj) { this.obj = obj; } public T get() { return obj; } public void set(T obj) { this.obj = obj; } }