In the end, it's just such a thing

Hello everyone, I’m 方圆
let’s take a look at final keywords


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!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/108058835