The Java final keyword:
1 Final data
1.1 Constants are common in two situations:
1. Constants during compilation
For example: Final int i1 = 0;
2. A value initialized during the run, but does not want it to change
For example: Final int i2 = (int) (Math.random () *20);
As above, use final for the base data type so that its value does not change.
But if you use final for an object handle, the meaning is different. Final will change the handle value to a constant, that is, the object that the handle points to cannot be changed, but the object itself is mutable.
It is important to note that the handle must be initialized to an object when declaring the final handle.
1.2 Blank final
Java allows the creation of blank final, such as: Final int i1; However, I1 needs to be initialized in the constructor, which does not violate the requirement that final must have a value.
Either initialize at the time of definition or initialize in the constructor. This guarantees that the final field will be properly initialized when it is used. And with the constructor initialization, it provides a lot of flexibility
1.3 Final Independent variable
We can set the argument to the final property by making the appropriate declarations in the argument list, such as public void Test (Final String str). This means that in the interior of a method, we cannot change what the argument handle points to.
Note: You can still assign a null (empty) handle to the final argument at this point. This is the same as the action we take with non-final arguments.
Packagemytest;classValue {inti = 1; } Public classtestfinal {Final intI0 = 8; Final intI1; Value v1=NewValue (); FinalValue v2 =NewValue (); Static FinalValue v3 =NewValue (); FinalValue v4; Final int[] A = {1, 2, 3, 4, 5, 6 }; Publictestfinal () {I1= 9; V4=NewValue (); V4.I= 9; } Final intI4 = (int) (Math.random () *20); Static Final inti5 = (int) (Math.random () *20); Public voidPrintFinalString ID) {//ID can be null//! id = "Hello";//error:can ' t change handleSystem.out.println (ID+ ":" + "I4 =" + I4 + ", i5 =" +i5); } Public Static voidMain (string[] args) {testfinal fd1=Newtestfinal (); //! fd1.i0++;//error:can ' t change value//! fd1.i1++;//error:can ' t change valueFD1.V1 =NewValue ();//OK--not finalfd1.v2.i++;//Object isn ' t constant! for(inti = 0; i < fd1.a.length; i++) Fd1.a[i]++;//Object isn ' t constant! //! fd1.v2 = new Value ();//error:can ' t change handle//! fd1.v3 = new Value ();//error:can ' t change handle//! fd1.a = new Int[3];//error:can ' t change handleFd1.print ("Fd1"); System.out.println ("Creating New FinalData"); Testfinal FD2=Newtestfinal (); Fd1.print ("Fd1"); Fd2.print ("Fd2"); Fd2.print (NULL); }}===================== OUTPUT =========================Fd1:i4= 6, i5 = 10CreatingNewFinaldatafd1:i4= 6, i5 = 10Fd2:i4= i5 = 10NULL: I4 = i5 = 10
2. Final method
The final + method generally means that the method cannot inherit from the quilt class, overriding
3. Final class
Final + class indicates that this class cannot be inherited
My Java Note------------final