forked from rootusercop/IMP-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
final finally finalize
25 lines (22 loc) · 1.23 KB
/
final finally finalize
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
final - keyword
finally - block written after try{} catch{} or only try{}. finally{} should have try{} or both try{} catch{}
before it. It cannot exist alone. Complier will throw Syntax Error. Hence finally{} CANNOT exist alone.
finalize - method called before System.gc() to free all the resources (i.e. references to objects are made null)
final:
final is a keyword. The variable decleared as final should be
initialized only once and cannot be changed. Java classes
declared as final cannot be extended. Methods declared as final
cannot be overridden.
finally:
finally is a block. The finally block always executes when the
try block exits. This ensures that the finally block is executed
even if an unexpected exception occurs. But finally is useful for
more than just exception handling - it allows the programmer to
avoid having cleanup code accidentally bypassed by a return,
continue, or break. Putting cleanup code in a finally block is
always a good practice, even when no exceptions are anticipated.
finalize:
finalize is a method. Before an object is garbage collected, the
runtime system calls its finalize() method. You can write system
resources release code in finalize() method before getting garbage
collected.