Declaring a Generic Class Wrapper<T> - Java Object Oriented Design

Java examples for Object Oriented Design:Generic Class

Description

Declaring a Generic Class Wrapper<T>

Demo Code

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;
  }
}

Result


Related Tutorials