Skip to content

Commit e68e1b7

Browse files
committed
feat: Demonstrate usage of final keyword with variables, methods, and initialization blocks
WHAT the code does: - Defines inner class TestFinal: - Method show() declared final, preventing overriding in subclasses. - Defines inner class TestFinal1 extending TestFinal: - Attempting to override show() would cause a compile-time error because final methods cannot be overridden. - Declares final instance variable PI: - Initialized inside an instance initialization block (allowed since it guarantees assignment before use). - Declares static final variable PI1: - Initialized inside a static block (allowed for static constants). - In main(): - Demonstrates declaring a final local variable (x). - Declares and initializes final local float PI, prints its value. WHY this matters: - The `final` keyword is versatile: 1. **Final variable**: becomes a constant; must be initialized exactly once. 2. **Final method**: cannot be overridden, preventing runtime polymorphism for that method. 3. **Final class**: cannot be subclassed, preventing inheritance. - Demonstrates different ways to initialize final fields: - Inline assignment. - Instance initialization block. - Static initialization block. - Clarifies that local final variables must be explicitly initialized before use. HOW it works: 1. Final method (show) → inherited by subclasses, but cannot be changed. 2. Final instance variable PI → initialized inside an instance block. 3. Final static variable PI1 → initialized in a static block. 4. Final local variable (PI in main) → initialized in method scope and cannot be reassigned. Tips and gotchas: - If a final variable isn’t initialized at declaration, it must be initialized in a constructor, instance block, or static block (depending on scope). - Declaring methods as final ensures consistency across subclasses but reduces flexibility; use sparingly. - Final classes (like java.lang.String) are immutable and safe from extension-based tampering. - Once assigned, final references cannot point to a new object, but the object itself may still be mutable unless explicitly designed immutable. Use-cases / analogies: - Final variables: like a birthdate — assigned once, never changed. - Final methods: like mandatory rules — children can follow them but not redefine them. - Final classes: like sealed envelopes — they cannot be extended or modified. - Initialization blocks: different “timing windows” when you can assign a constant before it’s used. Short key: java-final final-variable initialization-block static-block final-method final-class immutability. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b7738f7 commit e68e1b7

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
11
public class FinalKeyWord {
22

3-
//Final class can not the override or extends
4-
5-
class TestFinal
6-
{
7-
public final void show()
8-
{
3+
//Final class can not the override or extends.
4+
class TestFinal {
5+
public final void show() {
96
System.out.println("Hello");
107
}
118
}
12-
class TestFinal1 extends TestFinal
13-
{
9+
10+
class TestFinal1 extends TestFinal {
1411
//public void show(){} Must be a static to override the method.
1512
//We restrict run tie polymorphism means method overriding.
1613
}
1714

1815
//final float PI;
1916
//PI=3.1456F; Not allowed to initialize the variable
20-
21-
final float PI;
22-
{
17+
final float PI; {
2318
PI=3.145F; //Allowed instance block allowed.
2419
}
2520

2621
static final float PI1;
27-
static
28-
{
22+
23+
static {
2924
PI1=3.1456f; //Allowed this
3025
}
3126

32-
//Main method is static
33-
public static void main(String[] args)
34-
{
27+
public static void main(String[] args) {
3528
final int x=10; //Final identifier
3629

3730
final float PI; //Initialization of final
@@ -43,8 +36,12 @@ public static void main(String[] args)
4336
/*
4437
Summary of Key Points:
4538
46-
Final airBags() Method: This method in the Car class is declared final so it cannot be overridden in the EVCar class (or any other subclass). If you want subclasses to be able to override it, you need to remove the final keyword from the method.
39+
Final airBags() Method: This method in the Car class is declared final so it cannot be overridden in the
40+
EVCar class (or any other subclass).
41+
42+
If you want subclasses to be able to override it, you need to remove the final keyword from the method.
4743
Static Constant PI: PI is now a constant in the Car class and is declared final static (constant style).
4844
Naming Conventions: Classes are named using proper camel case (Car instead of car).
49-
EVCar Class: Demonstrates overriding of methods like accelerate() and decelerate(), but it cannot override airBags() due to the final modifier in the Car class.
50-
*/
45+
EVCar Class: Demonstrates overriding of methods like accelerate() and decelerate(), but it cannot override airBags()
46+
due to the final modifier in the Car class.
47+
*/

0 commit comments

Comments
 (0)