Final variable Usage summary in Java _java

Source: Internet
Author: User
Tags rand

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

Copy Code code as follows:

Final double PI = 3.14;

When the PI constant is used in a program, its value is 3.14, and the compiler will not accept the value of the constant that is defined as final in the program.
Variables defined by the final keyword must be assigned to them when they are declared. Final in addition to the constants that can modify the base data type, you can also decorate the object reference. Because an array can also be used as an object reference, final can decorate the array. Once an object reference is decorated to final, it can point to only one object at a constant and cannot be changed to another object. A field that is both static and final occupies only a certain amount of storage space that cannot be changed. The following example:

 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;//final defined variables must use uppercase letters to name the private static final int value_2 = 10;//and use underscores to connect; private fi 
  NAL 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 that is defined as final can only point to only one object and cannot point to other objects, but the value of an object itself can indeed be changed, so that a constant can be truly immutable by describing it as static final. The following example:

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 (a); 
  private static final int a2 = Rand.nextint (a); 
   
  public static void Main (string[] args) { 
    Finalstaticdata fdata = new Finalstaticdata ();//Instantiate an object 
    out.println (" Re-instantiate the value of the A1 object call: "+ fdata.a1"; 
    Out.println ("Re-instantiating the value of the object call A2:" + fdata.a2);   
    Finalstaticdata fdata2 = new Finalstaticdata ();//Instantiate the object 
    out.println ("The value of the re-instantiating object invocation A1:" + fdata2.a1); 
    Out.println ("Re-instantiating the value of the object call A2:" + Fdata2.a2); 
  } 
 

Re-instantiating an object call A1 value: 9
Re-instantiating an object call A2 value: 2
Re-instantiating an object call A1 value: 1
Re-instantiating an object call A2 value: 2

Summarize:

Defined as the final constant is not constant, the random number is given to the final variable, you can do every time the program to change the value of A1, but A2 is defined as static final form, so in memory for the A2 opened a constant region, When you instantiate a Fianlstaticdata object again, you still point to the memory area of A2, so the A2 value remains unchanged.

Skills:

Global constants are defined in Java, usually with the public static final decoration, which can only be assigned when defined.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.