If an instance variable is declared as 'final' , then the instance variable will behave like a constant by preventing its value from being modified.
Note - As per the coding convention, we've to mention the instance variables specified as 'final' in upper case as shown below:
final int COUNT = 5;
Lets implement this on Eclipse IDE:
1. Create a class 'FinalDemo' under any project as shown below:
2. Declare an instance variable 'COUNT' and specify it as 'final' as shown below:
3. Try to re-assign a different value to 'COUNT' variable which is specified as 'final' as shown below:
4. Observe that an error is displayed and hover your mouse over the error to read the error details as shown below:
Note - As per the coding convention, we've to mention the instance variables specified as 'final' in upper case as shown below:
final int COUNT = 5;
Lets implement this on Eclipse IDE:
1. Create a class 'FinalDemo' under any project as shown below:
4. Observe that an error is displayed and hover your mouse over the error to read the error details as shown below:
After looking at this error, its very clear that instance variables specified as 'final' can be assigned a value only once.
Note - 'final' is one of the non-access specifiers in Java.
Note - 'final' is one of the non-access specifiers in Java.