[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/CSapprenticeWT/OnJava8-Examples/master/generics/Holder.java [Back]  [Original]

// generics/Holder.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.Objects;

public class Holder {
  private T value;
  public Holder() {}
  public Holder(T val) { value = val; }
  public void set(T val) { value = val; }
  public T get() { return value; }
  @Override public boolean equals(Object o) {
    return o instanceof Holder &&
      Objects.equals(value, ((Holder)o).value);
  }
  @Override public int hashCode() {
    return Objects.hashCode(value);
  }
  public static void main(String[] args) {
    Holder apple = new Holder(new Apple());
    Apple d = apple.get();
    apple.set(d);
    // Holder Fruit = apple; // Cannot upcast
    Holder

Web Proxy Viewer  |  New URL  |  Original Page