🍀Finalization
🚨 The Final Frontier of Java Keywords 🚨
The dynamic trio of final
, finally
, and finalize()
serve unique purposes though they sound similar! Let's break it down:
The Immutable final
🔒
final
🔒📌 Rules of final
:
Declares immutable properties ❌🔀
Prevent overriding in subclasses 🚫
Commonly used for constants 📏
Key Benefits:
Avoid unintended changes 🙅♂️
Enforce architectural rules 👮
Improved understandability 💡
The Unstoppable finally
🚥
finally
🚥🧐 Purpose of finally
:
Guaranteed execution before method exit 🏁
Commonly closes resources 🗑️
Why use finally
?
finally
?Release external resources 🖇️
Restore program state 💾
Critical code that must run ✅
The Destructor finalize()
👋
finalize()
👋🔚 When finalize()
gets called:
Just before object is garbage collected 🗑️
Rarely used nowadays
try-finally
preferred
Downsides offinalize()
:
finalize()
:Not guaranteed to run 🤷♂️
Causes performance issues 🔥
Anti-pattern in most cases ❌
So while they sound similar, each handles unique responsibilities!
Last updated