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

Basic Generic Box Class

easyGenerics, Classes

📄 Code

Read carefully — what does this print?
1class Box<T> {
2 private T value;
3 
4 public Box(T value) {
5 this.value = value;
6 }
7 
8 public T getValue() {
9 return value;
10 }
11}
12 
13public class Main {
14 public static void main(String[] args) {
15 Box<String> strBox = new Box<>("hello");
16 Box<Integer> intBox = new Box<>(100);
17 
18 System.out.println(strBox.getValue());
19 System.out.println(intBox.getValue());
20 }
21}

🎯 Your Answer