When you use finalfor a variable, it means that it cannot be reassigned. Consider the following example:
public class FinalExample {
private final String a = "Hello World!";
private String b = "Goodbye World!";
public FinalExample() {
a = b;
b = a;
}
public static void main(String[] args) {
new FinalExample();
}
}
If you try to start it, you will receive an error message a=b:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field FinalExample.a cannot be assigned
at FinalExample.<init>(FinalExample.java:7)
at FinalExample.main(FinalExample.java:12)
, , , final String. , , , String , - String s = "Hello World!"; . , String s . , :
String s = "Hello World";
s = "Goodbye World!";
System.out.println(s);
Output: Goodbye World!
final, :
final String s = "Hello World";
s = "Goodbye World!";
System.out.println(s);
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final local variable s cannot be assigned. It must be blank and not using a compound assignment
final , , String. final , .