Java basics-final keyword

1. The final modified class cannot be inherited

Final keyword
1.final is a keyword of java language
2.final means final and immutable.
3. Final can modify variables and methods, as well as class
4. Final modified variables?
5. The method of final modification?
The final modified method cannot be overridden and cannot be rewritten .
6. The final modified class?
The final modified class cannot be inherited .
final class A{ } //Class B inherits Class A, which is equivalent to extending the functions of Class A. If you don't want others to extend Class A, you can give Class A the final keyword. In this case, Class A cannot inherit. class B extends A{ }




Insert picture description here

Such as the String class is the final class
Insert picture description here

2. The method of final modification cannot be overridden

class C{
    
    
	public final void dosome(){
    
    
		System.out.println("c的dosome");
	}
}
class D extends C{
    
    
	public void dosome(){
    
    
		System.out.println("d的dosome");
	}
}

Insert picture description here

3.final modify local variables

public static void main(String[] args) {
    
    
		//局部变量
		int i=100;
		//重新赋值
		i=200;
		
		//局部变量
		final int k=100;
		//重新赋值
		k=300;
	}

Local variables modified by final cannot be reassigned once they are assigned .

final int m;
		//第一次赋值
		m=200;
		//重新赋值
		m=300;

Insert picture description here
Important: final modified variables can only be assigned a value once.

4.final modified reference

The final modified variable can only be assigned a value once.
Is the reference a variable? ? ? ,Yes it is

public class FinalTest02 {
    
    
	public static void main(String[] args) {
    
    
		Person p1=new Person(20);
		System.out.println(p1.age);
		//代码不管怎么变化,p也是一个变量(只不过这里它有一个特殊的名字,引用)
		final Person p=new Person(30);
		p=new Person(30);
	}
}
class Person{
    
    
	int age;
	public Person(){
    
    }
	public Person(int age){
    
    
		this.age=age;
	}
}

Insert picture description here
Error: Unable to assign a value to the final variable p
Note: Local variables have no initial value.
Final modified reference:
The reference can only point to 1 object, and it can only point to that object forever. Can no longer point to other objects. And during the execution of the method, after the reference points to the object, the object will not be reclaimed by the garbage collector. The space will not be released until the current method ends.
Although the final reference points to A, it can no longer point to object B, but the data of object A can be modified.
Insert picture description here

5.final modified instance variables

The instance variables modified by final are
inseparable from their originality. The final modified variable can only be assigned once . (This sentence is easy to use everywhere)
Do you remember: If the instance variable is not manually assigned, the system will assign a default value .
The java programming is very good.
When is the instance variable assigned (initialized)
during the execution of the constructor method (assigned when new)

Ultimate conclusion: The
system is not responsible for assigning default values ​​to instance variables modified by final, and requires programmers to manually assign values ​​(assignments must be made when they are defined) .
This manual assignment can be assigned after the variable or in the constructor.

public class FinalTest03 {
    
    
	public static void main(String[] args) {
    
    
		
	}
}
class User{
    
    
	//编译器报错
//	final int age;
	//可以,因为程序员手动赋值了
	final double height=1.8;
	
	//以下这一堆代码全部联合起来,weight变量也是赋值了1次。
	//实例变量
	final double weight;
	//构造方法
	public User(){
    
    
		//只要我赶在系统赋默认值之前赋值就行。
		this.weight=80;
	}
}

6. Constant

Final modified instance variables
Final modified instance variables generally add static modification
Final modification:
Variables combined with static final modification are called
constants. It is recommended that all the names of constants be capitalized, and each word is underlined to connect the
constants: in fact, constants are the same as static variables. The difference is that the value of constants cannot be changed. Often they are stored in the method area, and they are initialized when the class is attached.
Constants cannot be re-assigned.
Constants are generally public and public.

public class FinalTest04 {
    
    
	public static void main(String[] args) {
    
    
		
	}
}
class Chinese{
    
    
	//身份证,每个人都不一样,对象级别的
	String idCard;
	//姓名,对象不同姓名不一样
	String name;
	//国家的值就是一个固定值,"中国"
	//既然这里的值不会改变,还有必要声明为实例变量吗
	//实例变量在堆中,一个对象一份,100个对象100份
	//实例变量既然使用final修饰了,说明该实例变量不会随着对象的变化而变化。
	//该实例变量前面应该添加:static关键字,变为静态的,存储在方法区。
//	static final String country="中国";
	static final String COUNTRY="中国";
	//i永远都是10,创建100个对象,i也是10
	//i是10是永远不会变化的,既然这样,没必要声明为实例变量,最好是静态的,节省内存空间。
			
}
class MyMath{
    
    
	public static final double PI=3.1415926;
}

7. Summary:

1. Final keywords

  • 1.1. The final modified class cannot be inherited.
  • 1.2. The final modification method cannot be covered.
  • 1.3. The final modified variable can only be assigned a value once.
  • 1.4. Once the final modified reference points to an object, it can no longer point to other objects, but the
    data inside the object pointed to by the reference can be modified.
  • 1.5. Final modified instance variables must be manually initialized, and system default values ​​cannot be used .
  • 1.6. Instance variables modified by final are generally used in conjunction with static and are called constants .
    public static final double PI = 3.1415926;

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/113829099