假设我们有一个 Student 类,其中有一个名为 Roll No.的字段。由于 Roll No 不应该在学生注册后更改,我们可以将其声明为类中的最终变量但我们无法初始化没有提前为所有学生滚动(否则所有学生都会有相同的卷号)。在这种情况下,我们可以声明 roll 无变量为空白 final,我们在对象创建期间初始化此值,如下所示:
```java
...
...
@@ -198,6 +199,7 @@ The type ABC cannot subclass the final class XYZ
```
**要记住的要点:**
1)[构造函数](https://beginnersbook.com/2013/03/constructors-in-java/"Constructors in Java – A complete study!!")不能被声明为 final。
2)本地最终变量必须在声明期间初始化。
3)[接口](https://beginnersbook.com/2013/05/java-interface/"Interface in java with example programs")中声明的所有变量默认为 final。
@@ -55,9 +55,11 @@ MyException Occurred: This is My error Message
```
**说明:**
你可以看到,在抛出自定义异常时,我在括号中给出了一个字符串(`throw new MyException("This is My error Message");`)。这就是为什么我的自定义异常类中有[参数化构造函数](https://beginnersbook.com/2014/01/parameterized-constructor-in-java-example/)(带有 String 参数)的原因。