Final: Finally, that's it.
In three different situations:
1. Decoration class
Indicates that the class cannot be inherited and extended
Final class A {} class extends A {}// Compiler error indicating that it cannot inherit
2. Modification method
Subclasses cannot overwrite and override this method
class a { finalvoid method () {}}classextends A { @ Override void method () {}// compiler Error, unable to override it }
3. Modifier variables
For an object, the reference to the variable is guaranteed to be unchanged and its value can be modified
For basic type data, the guaranteed value is the same. Same as when modifying method parameters
Public classFinaldemo { Public Static voidMain (string[] args) {String a= "Hello2"; FinalString B = "Hello"; String C= B + 2; System.out.println (A.equals (c)); String D= "Hello"; String e= d + 2; System.out.println (A.equals (e)); Modify the value of the current objectFinalStringBuffer Tempbuffer =NewStringBuffer ("Hello, world!"); Tempbuffer.append ("This is final string!"); //Tempbuffer = new StringBuffer ();//Unable to re-reference data }}
java-final keywords