Keyword final in Java

Keyword final in Java

1, Basic use of final

1. Decoration:

  • When the whole of a class is modified by final, it means that the class cannot inherit, and this class cannot have subclasses.
  • Note: all methods in the final class are implicitly final. Because they cannot be overridden, it is meaningless to add the final keyword to any method in the final class.

The two most important relationships in design patterns are continuation / implementation; The other is combinatorial relationship. When you encounter a class that cannot be inherited (final modification class), you should consider using composition. Enter the following code:

class Mystring{
	private string innerString;
	//Support old methods
	public int length(){
		return innerString.length();//Call the old method through innerString
	//Add new method
	public String toMyString(){

2. Modification method:

  • The private method is the final of the implicit square
  • The final method can be overloaded

private fina
All private methods in the class are implicitly specified as final. Since the private method cannot be accessed, it cannot be overridden. You can add the final keyword to the private method, but it's not good. Examples are as follows:

public class Base{
	private void test(){}

public class Son extends Base{
	public void test(){}
	public static void main(String[] args){
		Son son = new Son();
		Base father = son;

Both Base and son have method test(), but this is not an override. The method modified by private is implicit final, that is, it cannot be inherited, so it can not be overridden. The test() method in son belongs to a new member of son. Son transforms upward to get father, but father.test() cannot be executed. The test method in Base is private and cannot be accessed.

The final method can be overloaded

public class FinalExampleParent{
	public final void test(){
	public final void test(String str){

3. Modification parameters:

  • Java allows parameters to be specified as final declaratively in the parameter list, which means that you can change the object pointed to by the parameter reference in the method. This feature is mainly used to pass data to anonymous inner classes.

4. Modification variables:
Compile time constant and non compile time constant

public class Test{
	//compile-time constant 
	final int i = 1;
	final static int j = 1;
	final int[] a = {1,2,3};
	//Non compile time constant
	Random r = new Random();
	final int k = r.nextInt();
	public static void main(String[] args){

The value of k is determined by a random number object, so not all final modified fields are compile time constants, but the value of k cannot be changed after initialization.

static final
A static and final field only occupies a storage space that cannot be modified. It must be assigned at the time of definition, otherwise the compiler will not pass.

import java.util.Random

public class Test{
	static Random r = new Random();
	final int a = r.nextInt(10)+1;
	static final int b = r.static nextInt(10)+1;
	public static void main(String[] args){
		Test t1 = new Test();
		System.out.println("a = "+ t1.a + " b = "+ t1.b)
		Test t2 = new Test();
		System.out.println("a = "+ t2.a + " b = "+ t2.b)

The result of the above code is:

a = 2  b = 7
a = 8  b = 7

Calculation results: a is different, b is the same, and the reason is:
The field modified by static keyword is not an object, but belongs to this class. That is, the field modified by static final occupies only one space of memory, but will not be changed once initialized.

blank final
Java allows the generation of a blank final, that is, a field declared final but not given a fixed value, but assigned before the field is used

  • Assign a value at the definition (this is not called blank final)
  • Assign a value in the constructor to ensure that the value is assigned before it is used

This enhances the flexibility of final.

public class Test{
	final int a = 1;
	final int b;//Blank final
	public Test(){
		b = 2;
		public Test(int x){
			this.b = x;

The above code shows that the assignment of b is more flexible, but please note that if the field is modified by static and final, it can only be assigned at the definition, because the field does not belong to the object and belongs to this class.

2, Limitations and limitations of using final

  • When declaring a final member, its value must be set before the constructor exits
public class MyClass{
	private final int a = 1;
	public MyClass(){


public class MyClass{
	private final int a;
	public MyClass(){
		a = 1;
  • Declaring a member pointing to an object as final can only make the reference immutable, not the object.

The following method can still modify the list.

private final List list= new ArrayList();

Declaring final can ensure that the following operations are illegal

list = new ArrayList();
list= someOtherList;
  • If an object will be accessed in multiple threads and you do not declare its members final, you must provide other ways to ensure thread safety.

  • "Other methods" can include declaring the member as volatile, using synchronized or explicit Lock to control the access of all the members.

Summary of final keyword:

final can declare classes, member variables, methods, and local variables:

  1. Inherited classes, such as String class, are not allowed.
  2. Modifying referenced domain objects is not allowed.
  3. Overridden methods are not allowed, such as setter method of POJO class.
  4. Local variables that are reassigned during operation are not allowed.
  5. To avoid the repeated use of a variable in the context, use final to force the redefinition of a variable

Tags: Java

Posted on Thu, 07 Oct 2021 13:10:27 -0400 by FURQAN