-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample131.java
More file actions
29 lines (23 loc) · 797 Bytes
/
Example131.java
File metadata and controls
29 lines (23 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example 131 from page 101
//
class ErasedHold {
private Object contents;
public void set(Object x) {
contents = x; // Note: no cast
}
public Object get() {
return contents;
}
}
class Example131 {
public static void main(String[] args) {
ErasedHold h = new ErasedHold();
h.set("foo"); // Succeeds at run-time
System.out.println("Succesfully executed h.set(\"foo\")");
h.get(); // Succeeds at run-time
System.out.println("Succesfully executed h.get()");
// String s = h.get(); // Illegal, rejected by compiler
Integer i = (Integer)h.get(); // Legal, but fails at run-time
System.out.println("Succesfully executed Integer i = h.get()");
}
}