Java - What is the output: set type of unknown type?


What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    Wrapper<?> stringWrapper = new Wrapper<String>("Hello");
    Object str = stringWrapper.get(); 
  public static void printDetails(Wrapper<?> wrapper){

class Wrapper<T> {
  private T ref;

  public Wrapper(T ref) {
    this.ref = ref;

  public T get() {
    return ref;

  public void set(T a) {
    this.ref = a;

Click to view the answer

The method set(capture#2-of ?) in the type Wrapper is not applicable for the arguments (String)


The set(T a) method accepts the generic type argument.

This type, T, is not known to unknownWrapper, and therefore the compiler cannot make sure that the unknown type is a String type