SCJP 基本问题

telenths 2003-01-23 03:28:39
有一本材料上是这么说的
Final variables can be initialized only once within Java code. If the code attempts to reinitialize a final variable with another value, the compiler will complain. The following code is legal, even though the final variable is not initialized within the same line on which it is declared:

public final int users; // Does not initialize a final variable

但是我编译不通过
Test.java:3:variable users might not have been initialized

为什么 final 变量必须初始化?
public final int users;
...全文
22 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesmile979 2003-01-24
  • 打赏
  • 举报
回复
blank final就是这么用的,原因也没研究过。我说得对不对的?普通函数里也可以赋值的么?怎么想也不应该,应该会出错的啊。
telenths 2003-01-24
  • 打赏
  • 举报
回复
可是为什么可以在构造函数里给它赋值呢?

public final int users;
声明在 class level 里不时会自动赋给初值 0 吗;
既然是 final 怎么还能 在赋值呢?
telenths 2003-01-24
  • 打赏
  • 举报
回复
又查了一些材料总算是明白了
final 真是复杂啊

Final variables do not have to be initialized in the same line in which they are declared. Keep in mind that a final instance variable will not take the default value of zero. Examine the following code:


class FinalTest{
final int x; // Will not work
public void showFinal() {
System.out.println("Final x = " + x);
}
}


Attempting to compile the preceding code gives us the following:


c:\Java Projects\final>javac FinalTest.java
FinalTest.java:2: Blank final variable ‘x’ may not have been
initialized. It must be assigned a value in an initializer, or in
every constructor.
final int x;
1 error


We can assign a value to the variable x, even though it is not in the same line in which it was declared. This must be done in the constructor method or it will not work:


class FinalTest{
final int x; // Will work
public FinalTest() {
x = 28;
System.out.println("Final x = " + x);
}
}
tianfeichen 2003-01-24
  • 打赏
  • 举报
回复
类中的常量必须手动赋值,如果你留着让系统自动赋一个初始值,那你怎么改?

有三种方法:
一种就在声明的时候:
final int A = 5;

一种是在构造函数中:
final int A;
Constructor ()
{
A = 5;
}

一种是用一个instance initializer:
final int A;
{
A = 5;
}

方法中的常量只要在使用前赋值就可以了。
telenths 2003-01-23
  • 打赏
  • 举报
回复
我在试试看
bluesmile979 2003-01-23
  • 打赏
  • 举报
回复
普通函数里?不应该的啊。一定要在钩爪函数里给他值的啊,记错了???


ft.
telenths 2003-01-23
  • 打赏
  • 举报
回复
local level 是在 一个普通的函数里

>但是在使用之前一定要初始化
可是即使我不使用它 也无法编译通过
bluesmile979 2003-01-23
  • 打赏
  • 举报
回复
编译器强迫你一定得对所有final执行赋值动作---如果不是发生在其定义处,就德在每个钩爪函数中以表达式方式设定其值。还是为了保证final在使用前绝对会被初始化。因为钩爪函数肯定先得到执行。定义就是这样的。local level是在constructor里面吧???
bluesmile979 2003-01-23
  • 打赏
  • 举报
回复
The following code is legal,下面代码是合法的

even though the final variable is not initialized within the same line on which it is declared虽然在同一行里你并没有初始化他

但是,你总要初始化的。

这是在说空白final吧。就是说final变量定义的时候可以不给定初始值,但是在使用之前一定要初始化的。这句话只是说final的变量不必一定在定义的时候初始化
telenths 2003-01-23
  • 打赏
  • 举报
回复
这么说我分两行初始化也应该可以的

public final int users;
users = 10;

在 class level 通不过
local level 可以
为什么?
qxjavajava 2003-01-23
  • 打赏
  • 举报
回复
说的是只能初始化一次,可是你一次也没有初始化呀!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧