About keyword Final__java in Java

Source: Internet
Author: User
Tags modifier
keywords in Java finalLook at the final in the big Java, feel very interesting, so combined with the contents of the book and their own ideas to write down this article as a record and their understanding, if there is wrong hope to give corrections, common learning.

There are three possible uses of final: data, methods, classes

(i) Final data

Sometimes it is useful to define data as final:

A. An immutable compile-time constant (for a constant at compile time, the compiler can use constants to any place where they can be used, mitigating the burden of the runtime)

B. A value that is initialized at run time , and you don't want to change him.

We often see the definition of both static and final, which is defined as a domain that cannot change the storage space.

We use final for the base type, but what does the reference type define final?

It is also very simple that once the final reference is initialized to an object, it can no longer be changed to point to another object.

Here is a small example to illustrate:

Package com.edu.test;

Import Java.util.Random;
	class value{int i;
	Public Value (int i) {this.i = i;
	} public class finaltest{private static Random rand = new Random (47);
	Private String ID;
	Public finaltest (String id) {this.id = ID;
	//The following definition is compiled from a constant amount of private final int a1 = 1;
	private static int a2 = 2;
	
	The following defines the way that we see most often the public static final int a3 = 3;
	The following is defined as final, but it is not compiled from time to time private final int b1 = Rand.nextint (10);
	
	public static final int b2 = Rand.nextint (10);
	Private value value1 = new value (10);
	The following is the final definition of the reference type, private final Value value2 = new value (20);
	
	private static final Value Value3 = new value (30);
	
	With final modifier array private final int[] a = {1,2,3,4,5,6};
	Public String toString () {return id + ":" + "b1=" + B1 + ", b2=" + B2;
		public static void Main (String args[]) {finaltest ft1 = new Finaltest ("Obj1");
		
		The following is the wrong (can not change its value)//ft1.a1++;
		
		The following is the normal ft1.value1 = new Value (8); The following of course not, can not be instantiated with another object//ft1.value2 = new ValuE (8);
		
		
		Ft1.value3 = new Value (8);
		
		Although value2 is final, can not be another instantiation, but its own attributes are not final can change the ft1.value2.i++;		The following array is described, in fact, and the reference type is a property of the ft1.a[1]++;		Can change//ft1.a = new Int[5];
		A is a final array and cannot be System.out.println (FT1);
		SYSTEM.OUT.PRINTLN ("* * * * Create another object * * *");
		Finaltest ft2 = new Finaltest ("Obj2");
		System.out.println (FT1);
	System.out.println (ft2);
 }
}

Let's take a look at the results of the operation and there will be another harvest:

Obj1:b1=5,b2=8

Create another Object * * *

Obj1:b1=5,b2=8

Obj2:b1=3,b2=8

Why do I re-create an object FT2,B1 value changed, while B2 's value did not change.

The difference is that the B2 has a static modifier, emphasizing that only one copy is initialized and the point is not changed, so when the object is recreated, the B2 value does not change.

B1 and B2 are visible only at runtime when they can see the difference and are not visible during compilation.


We're going to instantiate the final variable of the object, to give an initial value, and sometimes we run into the so-called blank final, meaning it's clear that there's no initial value, but we have to have a value given before we use it.

There is also a final formal parameter, which is used in the anonymous inner class to pass data.


(ii) Final method

The method that cannot be changed is locking, in case any inheriting class modifies his meaning.

In fact, all private methods are implicitly specified as final, because in other classes can not be used, nature can not be overwritten.


(iii) Final class

Knowing the final method, the final class is also better understood and does not allow other classes to inherit this class.



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.