CJCoding With Joseph
15per day
← Back to Question List
Java Code Walkthrough #45

Generic Wrapper Describe Method

mediumGenerics, Classes

📄 Code

Read carefully — what does this print?
1class Wrapper<T> {
2 private T data;
3 
4 public Wrapper(T data) {
5 this.data = data;
6 }
7 
8 public String describe() {
9 return "Holds: " + data;
10 }
11}
12 
13public class Main {
14 public static void main(String[] args) {
15 Wrapper<Integer> w1 = new Wrapper<>(99);
16 Wrapper<Boolean> w2 = new Wrapper<>(true);
17 
18 System.out.println(w1.describe());
19 System.out.println(w2.describe());
20 }
21}

🎯 Your Answer