Problem:
final
is the only variable that cannot be modified after its initial declaration, it has nothing to do with access control.
Have a look below:
public class Test {
public static void main(String[] args) {
private int a = 5;
public int b = 10;
protected int c = 100;
static int p = 4;
final int i = 50;
}
}
You’ll get the compile-time error for each of those variables I used under my main function. Hence, the local variable doesn’t require any modifiers. So, the only applicable modifier for a local variable is final.
Let’s see another example:
public class Test {
public static void main(String[] args)
{
final int x;
System.out.println("Hello!");
}
}
The above program will print Hello! from our program. Here we’ve declared a final in x variable but not initialized. That doesn’t affect our program’s compilation.
I hope everything is clear to you now.
Thanks.