Java final keyword usage instructions

Source: Internet
Author: User

What are the final keywords modified?
Final makes the modified variable "unchanged", but because the object type variable is essentially a "reference", it also has two meanings: the reference itself remains unchanged, and the referenced object.

The reference itself remains unchanged:

Final StringBuffer a = new StringBuffer ("immutable ");

Final StringBuffer B = new StringBuffer ("not immutable ");

A = B; // compilation error

The referenced object remains unchanged:

Final StringBuffer a = new StringBuffer ("immutable ");

A. append ("broken! "); // Compiled

It can be seen that final is only valid for the referenced "value" (that is, the memory address of the object it points to), and it forces the reference to only point to the object initially pointed, changing the point will cause compilation errors. Final is not responsible for the changes to the objects it points. This is similar to the = operator: = operator is only responsible for the equal value of the referenced "value". As to whether the content of the object pointed to by this address is equal, the = operator does not matter.

Understanding final issues has important meanings. Many program vulnerabilities are based on this ---- final can only ensure that the reference always points to a fixed object, but cannot ensure that the state of that object remains unchanged. In multi-threaded operations, an object will be shared or modified by multiple threads. Unintentional modifications made by one thread to the object may cause another thread to crash.

View instances

//: FinalData. java
// The effect of final on fields

Class Value {
Int I = 1;
}

Public class FinalData {
// Can be compile-time constants
Final int i1 = 9;
Static final int I2 = 99;
// Typical public constant:
Public static final int I3 = 39;
// Cannot be compile-time constants:
Final int i4 = (int) (Math. random () * 20 );
Static final int i5 = (int) (Math. random () * 20 );
 
Value v1 = new Value ();
Final Value v2 = new Value ();
Static final Value v3 = new Value ();
//! Final Value v4; // Pre-Java 1.1 Error:
// No initializer
// Arrays:
Final int [] a = {1, 2, 3, 4, 5, 6 };

Public void print (String id ){
System. out. println (
Id + ":" + "i4 =" + i4 +
", I5 =" + i5 );
  }
Public static void main (String [] args ){
FinalData fd1 = new FinalData ();
//! Fd1.i1 ++; // Error: can't change value
Fd1.v2. I ++; // Object isn' t constant!
Fd1.v1 = new Value (); // OK -- not final
For (int I = 0; I <fd1.a. length; I ++)
Fd1.a [I] ++; // Object isn' t constant!
//! Fd1.v2 = new Value (); // Error: Can't
//! Fd1.v3 = new Value (); // change handle
//! Fd1.a = new int [3];

Fd1.print ("fd1 ");
System. out. println ("Creating new FinalData ");
FinalData fd2 = new FinalData ();
Fd1.print ("fd1 ");
Fd2.print ("fd2 ");
  }
}

I1 and I2 are basic data types with final attributes and contain values during the compilation period. Therefore, they can be used as constants during the compilation period, there will be no difference in any import method. I3 is a more typical way for us to experience such constant definitions: public indicates that they can be used outside the package; Static emphasizes that they only have one, and final indicates that it is a constant. Note that for fianl static basic data types that contain fixed initialization values (that is, compile-time constants), their names must be capitalized according to rules.

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.