In this post, we will see about final keyword in java. Final keyword can be associated with:
- variable
- method
- class
Final is often used when you want restrict others from doing any changes.
Table of Contents
Let’s go through each by one.
Final variable
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class FinalExample{ final int count=10; public void setCount() { count=20; } } |
Blank final variable
1 2 3 4 5 6 7 8 9 |
package org.arpit.java2blog; public class FinalExample { final int count; } |
You can initialize final variable once in a constructor as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class FinalExample { final int count; FinalExample(int count) { this.count=count; } public static void main(String args[]) { FinalExample fe=new FinalExample(10); System.out.println(fe.count); } } |
Let’s say you have Employee class and it has an attribute called empNo. Once object is created, you don’t want to change empNo.
So you can declare it final and initialize it in constructor.
static blank final variable
static blank final variable is static variable which is not initialized at the time of declaration. It can be initialized only in static block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class FinalExample { static final int count; static { count=10; } public static void main(String args[]) { System.out.println(FinalExample.count); } } |
When you run above program, you will get below output:
Final method
You can not override final methods in subclasses. You can call parent’s class final method using subclass’s object but you can not override it.
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 |
package org.arpit.java2blog; public class Shape{ public final void draw() { System.out.println("Draw method in shape class"); } } class Rectangle extends Shape { public void draw() { System.out.println("Draw method in shape class"); } public static void main(String args[]) { Rectangle rectangle= new Rectangle(); rectangle.draw(); } } |
You will get compilation error at highlighted row with text "Cannot override the final method from Shape".
If you remove draw method from rectangle class, it will work fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; public class Shape{ public final void draw() { System.out.println("Draw method in shape class"); } } class Rectangle extends Shape { public static void main(String args[]) { Rectangle rectangle= new Rectangle(); rectangle.draw(); } } |
When you run above program, you will get below output:
Final class
If you declare a class final, no other class can extend it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; final class Shape{ public final void draw() { System.out.println("Draw method in shape class"); } } class Rectangle extends Shape { public static void main(String args[]) { Rectangle rectangle= new Rectangle(); rectangle.draw(); } } |
You will get compilation error at highlighted row with text "The type Rectangle cannot subclass the final class Shape".
Summary for final keyword
- You can use final keyword with variable, method, and class.
- You can not use final keyword with constructor.
- If you do not initialize final variable(unless assigned in constructor), you will get compilation error.
- You can not change value of final variable once initialized.
- You can not override final methods in the sub class.
- You can not extend any final class.
You may also like:
Static keyword in java with examples