Final variables use summaries in Java

  • 2020-04-01 03:54:19
  • OfStack

The final keyword can be used for variable declarations, and once the variable is set, the value of the variable cannot be changed. Normally, variables defined in final are constants. Such as:


final double PI = 3.14; 

When the constant PI is used in the program, its value is 3.14.
The variables defined by the final keyword must be assigned to them at declaration time. Final can also modify object references in addition to modifying constants for primitive data types. Since an array can also be used as an object reference, the final can modify the array. Once an object reference is modified to final, it can only point to one object consistently and cannot be changed to another. A field that is both static and final occupies only one piece of storage that cannot be changed. The following cases:


import static java.lang.System.out; 
import java.util.Random; 
class Test{ 
  int i = 0; 
} 
public class FinalData { 
  static Random rand = new Random(); 
  private final int VALUE_1 = 9;//Variables defined in the final must be named using uppercase letters
  private static final int VALUE_2 = 10;//And use the underscore to connect;
  private final Test test = new Test(); 
  private Test test2 = new Test(); 
  private final int[] a = {1,2,3,4,5,6}; 
  private final int i4 = rand.nextInt(20); 
  private static final int i5 = rand.nextInt(20); 
  public String toString() 
  { 
    return i4 + "" + i5 + ""; 
  } 
  public static void main(String[] args) { 
    FinalData data = new FinalData(); 
    //data.test = new Test(); 
    //data.VALUE_2++; 
    data.test2 = new Test(); 
    for(int i=0; i<data.a.length; i++) 
    { 
      //a[i] = 9; 
    } 
    out.println(data); 
    out.println("data2"); 
    out.println(new FinalData()); 
    out.println(data); 
  } 
} 

An object defined as final can only point to a unique object and cannot point to other objects. However, the value of an object itself can indeed be changed. In order to make a constant truly unchangeable, the constant can be declared as static final.


import static java.lang.System.out; 
import java.util.Random; 
public class FinalStaticData { 
  private static Random rand = new Random(); 
  private final int a1 = rand.nextInt(10); 
  private static final int a2 = rand.nextInt(10); 
   
  public static void main(String[] args) { 
    FinalStaticData fdata = new FinalStaticData();//Instantiate an object
    out.println(" Re-instantiate the object invocation a1 The value of the :" + fdata.a1); 
    out.println(" Re-instantiate the object invocation a2 The value of the :" + fdata.a2);   
    FinalStaticData fdata2 = new FinalStaticData();//Instantiate the new object
    out.println(" Re-instantiate the object invocation a1 The value of the :" + fdata2.a1); 
    out.println(" Re-instantiate the object invocation a2 The value of the :" + fdata2.a2); 
  } 
} 

Re-instantiate the object to invoke the value a1 :9& PI;
Re-instantiate the object to call the value of a2 :2& PI;
Re-instantiate the object to invoke the value a1 :1& PI;
Re-instantiate the object to call the value of a2 :2& PI;

Conclusion:

Defined as final constant is not constant, gives the random number to final variables that can be done each time you run the program to change the value of the a1, but a2 is defined as the static final form, so the memory for the a2 has opened up a constant area, when instantiating an object FianlStaticData again, still point to a2 this memory area, so the value of the a2 stays the same.

Skills:

Global constants are defined in JAVA, usually with public static final, which can only be assigned at definition time.


Related articles: