1
Midterm Exam E01 Handout
for
e01
CS56 F17

Dog.java

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Dog {

private static Dog dogOfTheWeek = null;
private String name;

public void setAsDogOfTheWeek() {
dogOfTheWeek = this;
}

public static void resetDogOfTheWeek() {
dogOfTheWeek = null;
}

public static Dog getDogOfTheWeek() {
return dogOfTheWeek;
}

public Dog(String name) { this.name = name;}

public static void main(String [] args) {

Dog d1 = new Dog("Fido");
Dog d2 = new Dog("Princess");
Dog d3 = new Dog("Rover");
Dog d4 = new Dog("Snoopy");
Dog d5 = new Dog("Spot");
Dog d6 = d2;

d4.setAsDogOfTheWeek(); gc();
d5 = d2; gc();
Dog temp = d3; gc();
d3 = d5; gc();
d5 = temp; gc();
d3 = getDogOfTheWeek(); gc();
d1.setAsDogOfTheWeek(); gc();
d6 = null; gc();
d5 = null; gc();
d4 = null; gc();
d3 = null; gc();
d2 = null; gc();
d1 = null; gc();
temp = null; gc();
resetDogOfTheWeek(); gc();
}

public static void gc() {
// ... encourage garbage collection
}

}
End of Handout