The use of final keywords in Java and the difference between static final and final

Source: Internet
Author: User
Tags class definition rand
Java string optimization

The final keyword in Java can be used in class members (not called domains), in class methods, and in classes.

The final is used in the domain to represent:

1, a constant amount of compilation that never changes.

2, a value that is initialized at run time, and you don't want it to be changed.

The static keyword can be used in conjunction with the final keyword, and static emphasizes only one copy, regardless of how many times the class is initialized, the members of the static modifier are initialized only once. Final represents a constant and cannot be changed. Like the following example: View plain copy to clipboard print? File Finaldata.java import java.util.*; class value{int i; public Value (int i) {this.i=i;}} End of class Value public class finaldata{private static Random rand = new Random (a); private String ID; public Finald ATA (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 int I3=rand.nextint (20); Private final int i4=rand.nextint (20); static int int_5=rand.nextint (20); static final int int_6= Rand.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+ ":" + "i3 =" +i3+ ", I4 =" +i4+ ", int_5 =" +int_5+ ", int_6 =" +int_6;} " End of toString public static void Main (String [] args) {FinalData fd1 = new FinalData ("Fd1"); Fd1.valueone++;//error, can not change the final value of//fd1.valueone=15;//error, can not change the final value fd1.v2. i++; Fd1.v1=new Value (9); for (int j=0;j<fd1.a.length;j++) {fd1.a[j]++} System.out.println (FD1); SYSTEM.OUT.PRINTLN ("Creating new FinalData"); FinalData fd2 = new FinalData ("Fd2"); System.out.println (FD1); System.out.println (FD2); SYSTEM.OUT.PRINTLN ("Creating new FinalData"); FinalData fd3 = new FinalData ("fd3"); System.out.println (FD1); System.out.println (FD2); System.out.println (FD3); }//end of main}//end of class FinalData

The output results are:

Fd1:i3 = I4 = 1, int_5 = Int_6 = 15
Creating new FinalData
Fd1:i3 = I4 = 1, int_5 = Int_6 = 15
Fd1:i3 = 1, i4 = 9, int_5 = Int_6 = 15
Creating new FinalData
Fd1:i3 = I4 = 1, int_5 = Int_6 = 15
Fd1:i3 = 1, i4 = 9, int_5 = Int_6 = 15
Fd1:i3 = 8, i4 = 0, int_5 = int_6 = 15

It is easy to see from the result that as long as the class is initialized the final member is initialized, such as: I3,i4. The members of the static type, however, are not the same, whether final or not, are initialized only once, such as Int_5,int_6.

second, final used in the method

Final is used in the method for one reason: locking the method to prevent any inherited class from modifying its meaning.

Final is used in method adornments, and a secondary reason is that in earlier versions of Java, it is more efficient, but in the most recent Java version (Java SE5/6), the final method is no longer used for method optimization.

So the method is set to final only if you want to explicitly prohibit the method from being overwritten.

third, the class definition uses final (final Class)

When a class as a whole is defined as final, it is indicated that the class cannot be inherited.

The domain of the final class can be chosen to be final based on the individual's wishes (you can add or not add final before the member definition). The same rules apply to domains that are defined as final, regardless of whether the class is final or not. However, because the final class prohibits inheritance, all the methods in the final class are implicitly specified as final.

Related Article

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.