Java final and javafinal

Source: Internet
Author: User

Java final and javafinal

The final modifier variable indicates that the variable cannot be changed after initialization.

1. for basic types, the final value cannot be changed after modification.

1.

Final int;

A = 5;

2. final int a = 5;

2. For the reference type, the object referenced by final cannot be changed, but the content of the object can be changed.

In java, the final is used to modify the function parameters. However, the function body cannot modify the objects pointed to by this parameter, but the object content can still be modified.

Void f (final String str ){

Str = new String (); // not allowed

Str. toLowerCase (); // allow

}

Iii. final and static final

Public class T {

Final int a = new Random (10). nextInt ();

Static final int B = new Random (10). nextInt ();

Public static void main (String [] args ){

T t1 = new T ();

T t2 = new T ();

System. out. println (t1.a );

System. out. println (t1. B );

System. out. println (t2.a );

System. out. println (t2. B );

}

}

Result:

T1.a! = T2.a;

T1. B = t2. B;

Iv. Example

1,

Final int I = 0;

I ++; // allow

2,

Final String s = "abc ";

S. toLowerCase (); // allow

3,

Final int [] a = {1, 2, 3 };

A [1] = 1; // allow

A = new int [5]; // not allowed

 


Java final keyword explanation

Final means that the value cannot be changed for constants, for example, final int I = 100. The value of this I will always be 100. The variables are different, but the reference cannot be changed. For example, final File f = new File ("c: \ test.txt "); then this f must not be changed. If f itself has a method to modify the member variables, such as whether the variables are readable, modification is allowed. Image metaphor: A woman defines a final husband. Her career and income can be changed, but this woman will not change her husband.

What is the role and meaning of final in JAVA?

Final Method
If you declare the method as final, it means that you already know that the function provided by this method has met your requirements and does not need to be extended, this method cannot be overwritten by any class inherited from this class, but inheritance can still inherit this method, that is, it can be used directly. In addition, there is a mechanism called inline, which enables you to directly Insert the method subject into the call when calling the final method, rather than performing routine method calls, such as saving breakpoints, this may improve the program efficiency. However, when your method subject is very large, or you call this method in multiple places, your calling subject code will expand rapidly, which may affect the efficiency. Therefore, use final for method definition with caution.

Final class
When using final on a class, you need to consider it carefully. Because a final class cannot be inherited by anyone, it means that this class is a leaf class in an inheritance tree, in addition, such designs have been considered perfect without modification or expansion. For members in the final class, you can define it as final or not final. For methods, because the class is final, they naturally become final. You can also explicitly add a final to the methods in the final class, but this is obviously meaningless.

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.