-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.java
45 lines (38 loc) · 1.12 KB
/
Demo.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
public class Demo {
protected void finalize() throws Throwable {
try {
System.out.println("Inside finalize method of Demo Class.");
}
catch (Throwable e) {
throw e;
}
finally {
System.out.println("Calling finalize method of the Object class");
// Calling finalize() of Object class
super.finalize();
}
}
public static void main(String[] args) throws Throwable {
// Creating demo's object
Demo d = new Demo();
// Calling finalize of demo
d.finalize();
}
}
class Example
{
public static void main(String[] args)
{
Example ex = new Example(); // Creating object ex of class Example
ex = null; // Unrefrencing the object ex.
System.gc(); // Calling garbage collector to destroy ex
System.out.println("Unreferenced object ex is destroyed successfully!");
}
@Override
protected void finalize()
{
System.out.println("Inside finalize method");
System.out.println("Performing clean-up before destroying the object.");
System.out.println("Release and close connections.");
}
}