Java基础之关键字final详解
作者:From明 时间:2022-01-28 00:55:31
Java-关键字:final
1 .final可以用来修饰的结构:
类、方法、变量
2.final 用来修饰一个类:
此类不能被其他类所继承
比如:String类、System类、StringBuffer类
3.final 用来修饰方法:
表明此方法不可以被重写
比如:Object类中getClass();
4.final 用来修饰变量,此时的“变量”就称为是一个常量
4.1 final修饰属性:
可以考虑赋值的位置有:显示初始化、代码块中初始化、构造器中初始化
4.2 final修饰局部变量:
尤其是使用final修饰形参时,表明此形参是同一个常量。
当我们调用此方法时,给常量形参赋一个实参,一旦赋值以后
就只能在方法体内使用此形参,但不能进行重新赋值。
static final :用来修饰:全局常量
public class FinalTest {
final int WIDTH = 10;//显示初始化
final int LEFT;//代码块中赋值
final int RIGHT;
//final int DOWN;
{
LEFT = 2;
}
public void doweidth(){
//width = 20;//The final field FinalTest.width cannot be assigned
}
public void show(){
final int NUM = 10;//常量
//NUM+=20;
}
public void show(final int num){
//num = 20;编译不通过
System.out.println(num);
}
public FinalTest(){
RIGHT = 2;
}
public FinalTest(int n){
RIGHT = n;
}
//public void setDown(int down){
//this.DOWN = down;
//}
public static void main(String[] args) {
int num = 10;
num = num + 5;
FinalTest test = new FinalTest();
//test.setDown(3);
test.show(10);
}
}
final class FianlA {
}
class AA {
public final void show() {
}
}
class BB extends AA {
// public void show(){
//
// }
}
来源:https://blog.csdn.net/m0_46757622/article/details/116267451
标签:Java,关键字,final
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter 队列任务的实现
2023-07-07 17:25:14
Kotlin编程基础语法编码规范
2023-06-20 16:50:07
![](https://img.aspxhome.com/file/2023/6/105846_0s.jpg)
SpringBoot 利用thymeleaf自定义错误页面
2023-11-29 08:29:55
![](https://img.aspxhome.com/file/2023/4/60054_0s.png)
Android 如何获取手机总内存和可用内存等信息
2023-07-27 13:11:42
flutter ExpansionTile 层级菜单的实现
2023-06-15 16:04:01
![](https://img.aspxhome.com/file/2023/7/91007_0s.png)
IDEA+JRebel实现全自动热部署的方法步骤
2023-11-29 13:53:30
![](https://img.aspxhome.com/file/2023/4/60454_0s.jpg)
spring springMVC中常用注解解析
2023-09-14 20:45:46
Java按时间梯度实现异步回调接口的方法
2023-11-09 10:30:29
![](https://img.aspxhome.com/file/2023/2/59312_0s.png)
Java实现三子棋小游戏
2022-09-12 01:27:20
c#实现16进制和字符串之间转换的代码
2023-07-22 08:28:09
Maven+SSM框架实现简单的增删改查
2023-11-16 17:14:38
![](https://img.aspxhome.com/file/2023/0/59720_0s.png)
微信公众号服务号推送模板消息设置方法(后端java)
2023-11-20 08:27:58
![](https://img.aspxhome.com/file/2023/7/59487_0s.jpg)
javaWeb使用servlet搭建服务器入门
2023-11-21 04:47:45
IDEA 2020 本土化,真的是全中文了(真香)
2023-11-25 08:02:58
![](https://img.aspxhome.com/file/2023/6/60196_0s.png)
java获取当前时间的四种方法代码实例
2023-11-28 19:22:53
详解Spring注入集合(数组、List、Map、Set)类型属性
2023-11-28 00:16:53
Java实现考试系统
2023-11-18 04:15:03
![](https://img.aspxhome.com/file/2023/2/59602_0s.jpg)
Spring注解之@Lazy注解使用解析
2023-08-28 23:12:23
在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例
2023-11-25 09:49:02
IDEA Error:java:无效的源发行版:13的解决过程
2023-11-25 10:07:19
![](https://img.aspxhome.com/file/2023/8/59578_0s.png)