Commit e68e1b7
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
1 file changed
+16
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
12 | | - | |
13 | | - | |
| 9 | + | |
| 10 | + | |
14 | 11 | | |
15 | 12 | | |
16 | 13 | | |
17 | 14 | | |
18 | 15 | | |
19 | 16 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 17 | + | |
23 | 18 | | |
24 | 19 | | |
25 | 20 | | |
26 | 21 | | |
27 | | - | |
28 | | - | |
| 22 | + | |
| 23 | + | |
29 | 24 | | |
30 | 25 | | |
31 | 26 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 27 | + | |
35 | 28 | | |
36 | 29 | | |
37 | 30 | | |
| |||
43 | 36 | | |
44 | 37 | | |
45 | 38 | | |
46 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
47 | 43 | | |
48 | 44 | | |
49 | | - | |
50 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
0 commit comments