June 2018
Beginner
722 pages
18h 47m
English
A final variable is a variable that, once initialized, cannot be assigned to another value. It is denoted by the final keyword:
void someMethod(){ final int x = 1; x = 2; //generates compilation error //some other code}
Nevertheless, the following code will work just fine:
void someMethod(){ final int x; //Any code that does not use variable x can be added here x = 2; //some other code }
The preceding code does not generate a compilation error because the local variable is not initialized to a default value automatically in a declaration statement. Only the class, instance variable, or array component is initialized to a default value if the variable is not initialized explicitly (see the Primitive types and literals ...
Read now
Unlock full access