Hello everyone, I’m
方圆
let’s take a look at final keywords
table of Contents
1. Timing of assignment
1.1 Non-static properties
- The first is after the declared variable
直接等号赋值
- The second is in
构造函数中赋值
- The third is to use the initial code block assignment (see you for the first time)
public class FinalDemo {
//1
private final int demo = 3;
//2
public FinalDemo(int demo) {
this.demo = demo;
}
//3
{
demo = 3;
}
}
1.2 Static properties
- The first is in the declaration
直接等号赋值
- The second is in
静态代码块中赋值
public class FinalDemo {
//1
private static final int demo = 3;
//2
static {
demo = 3;
}
}
Also note that the above is the demo code, final can only be assigned once, this is just to make it look easy
1.3 local variables
Local variables are very simple, just like we use ordinary variables, as long as we 使用之前进行赋值
can
2. Final modification method
构造方法
Cannot be modified by final- Methods modified by final
不能被重写
static修饰的方法
Can't be rewritten either! ! !
3. Final modified class
- The final modified class cannot be inherited. For example, the familiar String class is final modified
4. Final modification attributes
- The final modified
基本数据
type cannot be changed - Modified by final,
引用类型
it引用
cannot be changed, but its attributes can be changed
Come on!