![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
You can declare a variable in any scope to be final, including parameters to methods and constructors. The value of a final variable cannot change after it has been initialized.
To declare a final variable, use the
final
keyword in the variable declaration before the type:The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value tofinal int aFinalVar = 0;aFinalVar
result in a compiler error. You may, if necessary, defer initialization of a final local variable. Simply declare the local variable and initialize it later, like this:A final local variable that has been declared but not yet initialized is called a blank final. Again, once a final local variable has been initialized it cannot be set and any later attempts to assign a value tofinal int blankfinal; . . . blankfinal = 0;blankfinal
result in a compile-time error.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |