Final Variables in JAVA
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
Final in aFinalVar=0;
The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value to aFinalVar result in a compiler error. You may, if necessary, defer initialization of a final variable. Simply declare the variable and initialize it later, like this:
Final int blankfinal;
….
Blankfinal =0;
A final variable that has been declared but not yet initialized is called a blank final. Again, once a final variable has been initialized it cannot be set and any later attempts to assign a value to blankfinal result in a compile-time error.
No comments:
Post a Comment