-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakRefexample.java
More file actions
35 lines (26 loc) · 927 Bytes
/
WeakRefexample.java
File metadata and controls
35 lines (26 loc) · 927 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
29
30
31
32
33
34
35
import java.lang.ref.WeakReference;
class exceptions {
void print() {
System.out.println("Print method called");
}
}
public class WeakRefexample {
public static void main(String[] args) {
// Strong Reference
exceptions obj = new exceptions();
obj.print();
// Weak Reference has explicit type class[exceptions] of Reference Object[obj]
WeakReference<exceptions> weak = new WeakReference<>(obj);
obj = null;
exceptions obj1 = weak.get();
/**
*
* get() function : It returns this reference object's referent.
* If this reference object has been cleared,
* either by the program or by the garbage collector,
* then this method returns null.
*
**/
obj1.print(); // will call print method and will not throw any exceptions
}
}