Final variables in Java
🔷 Final Variables in Java
✅ What is final
in Java?
The final
keyword is used to declare constants. Once assigned, a final variable's value cannot be changed.
📌 Types of Final Usage:
-
Final variable
-
Final method
-
Final class
✅ 2.1 Final Variable
-
Value is assigned only once.
-
If not initialized during declaration, it must be initialized in a constructor or static block.
🔹 Example:
✅ 2.2 Final Method
-
Cannot be overridden by subclasses.
✅ 2.3 Final Class
-
Cannot be extended (no subclassing).
Comments
Post a Comment