当前位置: 代码迷 >> 综合 >> 对于final理解
  详细解决方案

对于final理解

热度:16   发布时间:2024-03-10 01:22:59.0

对于final 的理解

final修饰变量:为常量
final修饰方法:不可重写
final修饰类:不可被继承

1.修饰变量时,基本数据类

一旦初始化,则无法更改

2.引用类型

以下图为例
当我们创建

final Student student = new Student1111"name");

我们进行修改student的值是没有问题的,比如

student.setId(2333);

不会报错,id也会改变,但是如果你进行更改指向的地址

student = new Student();

会报错,不允许修改地址

在这里插入图片描述

3. final不能修饰interface

因为interface接口只能被 public修饰,当你强行用final修饰,会提示你,接口行的非法修饰符;只允许public&abstract
在这里插入图片描述

  相关解决方案