What is the final keyword in Java?
| Used on | Meaning |
|---|---|
| Variable | The value can be set only once (a constant) |
| Method | Can not be overridden in a subclass |
| Class | Can not be extended (for example String is final) |
| Parameter | Can not be changed inside the method |
public class Final {
public static void main(String[] args) {
final int max = 10;
max = 20;
}
}
Output
Final.java:4: error: cannot assign a value to final variable max
max = 20;
^
1 error
error: compilation failed
A final reference can not point to another object, but the object itself can still change. See also final, finally and finalize.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |