static、static final、final修饰的变量的区别
时间: 2023-09-27 21:07:22 浏览: 49
`static`、`static final`和`final`是Java中用于修饰变量的关键字,它们有以下区别:
1. `static`:用于创建静态变量。静态变量属于类,而不是实例化对象。它在类加载时就会被初始化,并且所有该类的实例共享相同的静态变量。可以通过类名直接访问静态变量。
2. `final`:用于声明一个常量,即值不能被修改的变量。`final`修饰的变量只能被赋值一次,并且在声明时或构造函数中赋值。一旦赋值后,其值就不能再改变。
3. `static final`:用于声明一个静态常量。静态常量是类级别的常量,它在类加载时就会被初始化,并且所有该类的实例共享相同的静态常量。与普通的`final`变量不同,静态常量一般使用全大写字母来命名。
总结:
- `static`用于创建静态变量,属于类级别,所有实例共享。
- `final`用于声明常量,一旦赋值后不能再修改。
- `static final`用于声明静态常量,属于类级别,所有实例共享,一旦赋值后不能再修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)