Final modifier Flashcards
What entities can the Final modifier be applied to?
variables | methods | classes
…
🧠️ [Watter&Oil Rule]
🤯️⚠️📣️ Final and Abstract modifiers are NOT compatible. (Instances/Abstract Classes|method)
What does a final variable means?
final variable means CONSTANT:
primitive-variable -> CONSTANT-VALUE
referenced-variale -> CONSTANT-REFERENCE
What does a final method means?
It can NOT be OVERWRITTEN
What does a final class means?
It can NOT be EXTENDED
Mention some FINAL LOCAL-VARIABLE ⚠️pitfalls⚠️…
🧠️ COULD BE initialized
…..
A final local-variable COULD be initialized just once
✅ If NO initialized BUT NO used is ok
❌ If USED but NO initialized ⚠️WON’T COMPILE⚠️
Mention some FINAL INSTANCE-VARIABLE ⚠️pitfalls⚠️… (2 + BONUS)
🧠️ HAVE TO BE initialized (Just once)
🧠️ NON-STATIC ❌ STATIC CONTEXT
:::::…..
A final instance-variable HAVE TO be initialized
❌ If NO initialized ⚠️WON’T COMPILE⚠️
⚠️❌ non-static variable cannot be referenced from a static context
🕵♂️️ Could be initialized from multiple constructors (Watch out if calling this() leads to reassign value)
What are the INITIALIZATION-RULES for instance & static variables?
(inline | initializers | static-initializers | constructors
✅|✅|❌|✅* -> instance
✅|✅|❌|⚠️* -> instance final ::
✅|✅|✅|✅ -> static ::
✅|❌|✅|❌ -> static final ::
💾️🧠️🕵♂️️ static-context RULE
💾️🧠️🕵♂️️ final-instance variables HAVE TO be initialized
Mention some FINAL-METHOD ⚠️pitfalls⚠️… (2 + Bonus)
⚠️Can NOT be OVERWRITTEN
💾️🧠️🕵 RULE[watter&oil]
*** 🤓️🤷♂️️ An abstract method could be implemented as final ✅
Mention some FINAL-CLASSES ⚠️pitfalls⚠️… (2 + Bonus)
🤯️⚠️📣️ A final class cannot be extended. 💾️🧠️🕵 RULE[watter&oil] -> (Interfaces/Abstract classes)
📝️ What does immutability means?
Immutability means an Object instance can’t be modified after it’s creation
🤯️⚠️📣️ Immutability concept is applicable to Object instances only.
Important RULES
💾️🧠️🕵♂️️ RULE[watter&oil] final and abstract modifiers are NOT compatible
💾️🧠️🕵♂️️ RULE[static-context] non-static variable cannot be referenced from a static context