Java Basics
Syntax
"final" Keyword
- The final keyword's functionality changes based on its usage.
Variable
final int varName = 5;
varName = 6;
- Variables with the final keyword cannot be changed. They are similar to const in C++.
- They can only be declared once. Unlike C++, they can remain undeclared until otherwise.
final int varName;
varName = 6;
Within a Class
class Animal {
final void create() {
// do something
}
}
class Dog extends Animal {
void create() {} // NOT ALLOWED
}
- When used within a class, functions or variables with using final cannot be overrode. "create" being declared within Animal means that a class that extends Animal cannot implement their own create function and must use its superclass function.