C
C
Core Java
Search…
21) Final Keyword
Final:
    1.
    Is a keyword.
    2.
    The final keyword can be used in many contexts.
final can be:
Variable
Method
Class
    1.
    If you want to declare constant variables where in the value cannot change, then those variables should be declared with the keyword “final”.
    2.
    Final variable value cannot be changed or overridden.
    3.
    Both local and global variable can be final.
    4.
    Global final variable should be initialized at the time of initialization itself.
    5.
    Local final variable can be declared once and initialized later.
    6.
    In terms of inheritance, final keyword us used to avoid overriding.
    7.
    Final class cannot be inherited.
    8.
    Final class methods cannot be overridden because for method overriding inheritance is mandatory.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class A {
4
public static void main(String[] args) {
5
final int i = 10;
6
i = 20;
7
System.out.println(i);
8
}
9
}
Copied!
Output:
Note:
    If you make a variable as final then we can never re-initialize that variable.
    Static and non-static variable if made Final then its initialization is mandatory or else we will get blank field error.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class B {
4
final int j;// error because not initialized
5
6
public static void main(String[] args) {
7
final int j;// local variables should be initialized before usage System.out.println(j);
8
}
9
}
Copied!
Output:
Exception in thread "main" java.lang.Error: Unresolved compilationproblem: The local variable j may not have been initialized
Note:
In case of local variable make it final there is no error, but using it without initialization will throw error.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class E {
4
public void test(final int i) {
5
i = 30;// The final local variable i cannot be assigned. It must be blank and not using
6
// a compound assignment
7
System.out.println(i);
8
}
9
10
public static void main(String[] args) {
11
E e1 = new E();
12
e1.test(10);
13
}
14
}
Copied!
Output:
The final local variable i cannot be assigned. It must be blank and not using a compound assignment
Note: If you make an array as final then its size cannot be altered.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class F {
4
public static void main(String[] args) {
5
final int[] a = new int[5];
6
a[0] = 10;
7
a[0] = 20;
8
System.out.println(a[0]); // 20
9
}
10
}
Copied!
Output:
20
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class G {
4
public static void main(String[] args) {
5
final int[] a = new int[5];
6
a = new int[2];
7
}
8
}
Copied!
Output:
Note:
    1.
    When array is final, its size cannot be changed.
    2.
    When array is not final, then size can be altered.
    3.
    When (String[] args) is final, then its size cannot be altered. args[] is a take string no number allowed
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
public class H {
4
public static void main(String[] args) {
5
args = new String[3];
6
args[0] = "10";
7
System.out.println(args[0]);
8
}
9
}
Copied!
Output:
10
Note:
    1.
    Inheritance of final class is not possible.
    2.
    Final Variable value cannot be altered.
    3.
    Final Array size cannot be altered, but value can be altered.
    4.
    Final Class, inheritance is not possible and value cannot be inherited.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
final class J {
4
int i = 10;
5
}
6
7
final class I extends J // the type I cannot sub class to final
8
{
9
public static void main(String[] args) {
10
J j1 = new J();
11
System.out.println(j1.i);
12
}
13
}
Copied!
Output:
Unresolved compilation problem the type I cannot sub class to final
Note: A final class can inherit the members of non-final class.
Example:
1
package com.gs.ilp.corejava.finalKeyword;
2
3
class L {
4
int i = 20;
5
}
6
7
final class K extends L {
8
public static void main(String[] args) {
9
L l1 = new L();
10
System.out.println(l1.i);
11
}
12
}
Copied!
Output:
20
Note:
If you make method as final, meaning that sub classes cannot override this method. The compiler checks and gives an error if you try to override the method.
When we want to restrict overriding then make a method as a final.

Example:

1
package com.gs.ilp.corejava.finalKeyword;
2
3
class XYZ {
4
final void demo() {
5
System.out.println("XYZ Class Method");
6
}
7
}
8
9
public class ABC extends XYZ {
10
void demo() {
11
System.out.println("ABC Class Method");
12
}
13
14
public static void main(String args[]) {
15
ABC obj = new ABC();
16
obj.demo();
17
}
18
}
Copied!
Output:
The above program would throw a compilation error as we cannot override a final method.
Last modified 2yr ago
Copy link