Junior — Middle
What is the purpose of the Final modifier in Java, and in what cases is it used?
sobes.tech AI
Answer from AI
The final modifier in Java is used to restrict modifications:
- For variables: the value can be assigned only once, after which it becomes a constant.
- For methods: the method cannot be overridden in subclasses.
- For classes: the class cannot be inherited.
Example of using final for a variable:
final int MAX_USERS = 100;
final is used to enhance code safety, prevent accidental changes, and ensure immutability.