forked from JFulgoni/Java-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalexample.java
More file actions
57 lines (51 loc) · 1.13 KB
/
Copy pathFinalexample.java
File metadata and controls
57 lines (51 loc) · 1.13 KB
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
51
52
53
54
55
56
57
package john_test;
public class Finalexample {
public void finallyFunction(){
int[] array = new int[]{1,2,3, 4, 5};
int sum = 0;
try{
for(int i = 0; i < 6; i++){
sum += array[i];
}
}
catch (IndexOutOfBoundsException e){
System.out.println(e);
}
/*
* Any statement in finally will run regardless of the outcome of try/catch
* It must be put after the catch statement
*/
finally{
System.out.println("Done");
}
}
@Override
protected void finalize() throws Throwable{
try{
System.out.println("Finalize");
//removes resources from this class
}
catch (Throwable t){
throw t;
}
finally{
super.finalize();
}
}
public static void main(String[] args){
/*
* Can make variables final, which means their value can't be changed in the rest of the program
* Can also make functions final, which means it can't be overridden by subclasses
*/
final int x = 5;
System.out.println(x);
Finalexample fe = new Finalexample();
fe.finallyFunction();
try {
fe.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}