-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalKeywords.java
More file actions
102 lines (86 loc) · 2.34 KB
/
FinalKeywords.java
File metadata and controls
102 lines (86 loc) · 2.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Examples;
/* KEEP IN MIND: Always initialize Blank final vars in Class constructor !! */
// Blank Final Variable Example: 1
/*
public class FinalKeywords {
final int num;
FinalKeywords(){
num = 10;
}
void sumUp() {
System.out.println(num);
}
public static void main(String[] args) {
FinalKeywords foo = new FinalKeywords();
foo.sumUp();
}
}
*/
// Blank Final Variable Example: 2
/*
public class FinalKeywords {
final int rollNo;
FinalKeywords(int rNum){
rollNo = rNum;
}
public void studentNo() {
System.out.println("Student Number is: " + rollNo);
}
public static void main(String[] args) {
FinalKeywords student1 = new FinalKeywords(2002);
FinalKeywords student2 = new FinalKeywords(1282);
student1.studentNo();
student2.studentNo();
}
}
*/
// Uninitialized Static Variables
/*
public class FinalKeywords {
static String name;
static{
name = "Fatal";
}
public static void main(String[] args) {
System.out.println(FinalKeywords.name);
}
}
*/
// Final Methods (Can not be overridden by child class)
/*
class parentClass {
final public void display() {
System.out.println("This is Final method of Parent Class");
}
}
public class FinalKeywords extends parentClass{
public static void main(String[] args) {
parentClass Obj = new parentClass();
Obj.display();
}
}
*/
// Final Classes (Can not be extended by other classes)
/*
final class parentClass {
public void overRiding() {
System.out.println("Parent Class Method");
}
}
public class FinalKeywords extends parentClass{ // Here it throws the error (As Final Class can not be Inherited)
public static void main(String[] args) {
}
}
*/
// Final Parameters (Can not be modified later)
public class FinalKeywords {
public void details(int num, final String name) {
num = num + 2; // Can be changed, as it is not final
// name = name + "rew"; // A Final Variable can not be modified, once assigned (As a Parameter)
System.out.println("Name: " + name + "; Number: " + num);
}
public static void main(String[] args) {
FinalKeywords Obj1 = new FinalKeywords();
Obj1.details(8, "Cee");
}
}