The role of using final to modify parameters in Java
Adding the final keyword before the method parameters is to prevent the data from being modified in the method weight.
There are two main situations: first, use final to modify the basic data type; second, use final to modify the reference data type.
In the first case, modify the basic data type, at which time the value of the parameter cannot be modified in the method body, that is, it cannot be reassigned. Otherwise, the compilation will not be passed.
The second case is to modify the reference type. At this time, the object referenced by the parameter variable cannot be changed. However, for reference data types, it is perfectly OK to modify their properties.
Therefore, if you want to use the keyword final, use the basic data type, which is very useful.
Final variable:
Use final for basic types: it is a constant, the value is constant and the value remains unchanged.
Use final for object references: make the reference constant and once the reference is initialized to point to an object, it cannot be changed to point to another object. However, the object itself can be modified, and Java does not provide a way to make any object constant. This limitation also uses arrays, which are objects.
example:
class Value{ int i; public Value(int i){ this.i = i; }}public class FinalData { private static Random random = new Random(47); private String id; public FinalData(String id){ this.id = id; } private final int valueOne = 9; private static final int VALUE_TWO = 99; public static final int VALUE_THREE = 39; private final int i4 = random.nextInt(20); static final int INT_5 = random.nextInt(20); private Value v1 = new Value(11); private final Value v2 = new Value(22); private static final Value VAL_3 = new Value(33); private final int[] a = {1, 2, 3, 4, 5, 6}; public String toString(){ return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5; } public static void main(String[] args) { FinalData fd1 = new FinalData("fd1"); //! fd1.valueOne++; // Because valueOne is a basic type constant, its value is constant fd1.v2.i++; // The content of the object modified by final can be changed fd1.v1 = new Value(9); for(int i = 0; i < fd1.a.length; i++) fd1.a[i]++; //! fd1.v2 = new Value(0); // Because v2 is a reference type modified by final, its reference cannot be modified to point to another object//! fd1.VAL_3 = new Value(1); // It occupies a memory space that cannot be changed//! fd1.a = new int[3]; // Final modified array System.out.println(fd1); System.out.println("Creating new FinalData"); FinalData fd2 = new FinalData("fd2"); System.out.println(fd1); System.out.println(fd2); }}/*output:fd1: i4 = 15, INT_5 = 18Creating new FinalDatafd1: i4 = 15, INT_5 = 18fd2: i4 = 13, INT_5 = 18*/analyze:
For fd1, fd2 two objects, i4 is unique, that is, each object has an i4, but INT_5 is declared static, that is, it is shared by the class, fd1 and fd2 share INT_5, which is initialized at the time of loading, rather than initializing every time a new object is created (for example, i4); but it is set to final at the same time, so its reference is unchangeable, that is, it cannot be modified to point to another object.
Blank final:
is declared as final but has no initial value given. Final must be assigned using expressions in the domain definition or in each constructor, which is why the final domain is always initialized before use.
Final parameters:
This means you can't change the parameter reference in the method to point to another parameter, but you can modify what the final object points to
example:
class Gizmo{ int i = 0; public void spin(){}}public class FinalArguments { void with(final Gizmo g){ //! g = new Gizmo(); // The reference modified by final is not modified to point to another object g.i++; // But the content pointed to by the final object can be modified} void without(Gizmo g){ g = new Gizmo(); g.spin(); } // int g(final int i){// //! i++; // Because parameter i is a constant value// } int g(final int i){ return i + 1; } public static void main(String[] args) { FinalArguments bf = new FinalArguments(); bf.with(null); bf.with(null); }}analyze:
The parameter is declared as final. If it is a basic parameter, it is a constant and cannot be modified; if it is a reference variable, it cannot be modified to point to another object, but the content of the object referred to by the reference can be modified.
fianl method:
Reason for use:
All private methods in the class are implicitly specified as final, and since the private method cannot be used, it cannot be overwritten. A final modifier can be added to the private method, but this does not give any additional meaning to the method.
example:
class WithFinals{ private final void f(){ System.out.println("WithFinals.f()"); } private void g(){ System.out.println("OverridingPrivate.f()"); }}class OverridingPrivate extends WithFinals{ private final void f(){ System.out.println("OverridingPrivate.f()"); }}class OverridingPrivate2 extends OverridingPrivate{ /* * When using the Override annotation to force the f() method to overwrite the f() method of the parent class, an error will be reported* because it does not know whether the parent class has the method. For the g() method, it just generates a new method, and * does not overwrite the g() method in the parent class. */ //@Override public final void f(){ System.out.println("OverridingPrivate2.f()"); } public void g(){ System.out.println("OverridingPrivate2.g()"); }}public class FinalOverridingIllusion{ public static void main(String[] args) { OverridingPrivate2 op2 = new OverridingPrivate2(); op2.f(); op2.g(); // You can transform upward OverridingPrivate op = op2; //! op.f(); // The final method in the parent class is invisible to the subclass //! op.g(); WithFinals wf = op2; // wf.f(); // wf.g(); }}/*output:OverridingPrivate2.f() OverridingPrivate2.g()*/analyze:
When does the coverage occur:
1. Methods that appear in subclasses that are exactly the same as those of parent classes
2. A subclass can be transformed into a parent class upward and call the method in the parent class
If a method in the parent class is declared as final or private, then this method is invisible to the subclass. Even if a method exactly the same as the parent class is created in the subclass, this is a new method, rather than a method overridden from the parent class.
Final class:
That is, this class cannot be inherited, whether it is you or others, that is, this class does not require any changes, nor does it require any subclasses, such as the String class.
example:
class SmallBrain{}final class Dinosaur{ int i = 7; int j = 1; SmallBrain x = new SmallBrain(); void f(){}}// error: The type Further cannot subclass the final class Dinosaur// Dinosaur class cannot have subclasses// class Further extends Dinosaur{}public class Jurassic { public static void main(String[] args) { Dinosaur n = new Dinosaur(); nf(); ni = 40; n.j++; }}Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.