这么说我分两行初始化也应该可以的public final int users;
users = 10;在 class level 通不过
local level 可以
为什么?
users = 10;在 class level 通不过
local level 可以
为什么?
解决方案 »
- 请指点一下有关 IP解析的代码
- 如何判断request为空
- 【新手提问】请问为什么数据库的表里面没数据,但是返回的结果集确是真呢?
- 如何将一个由1和0组成的String转换成十进制数?
- String s="123456789";String[] ss = s.split("");为什么ss.length是10不是9呢
- 高手请进:关于Socket的问题。
- 我用jbuild生成了exe文件的问题???急急急急
- Sun One Studio 4 有人用过吗?
- 我的applet在client端的computer上不能正常显示,怎么办?
- 用NetBeans8.0.1启动tomcat7失败
- 那位大侠能详细讲一下JDK安装目录下的各个目录及其文件的作用?
- 帮忙看一下,一个很简单的问题。
可是即使我不使用它 也无法编译通过
声明在 class level 里不时会自动赋给初值 0 吗;
既然是 final 怎么还能 在赋值呢?
一种就在声明的时候:
final int A = 5;一种是在构造函数中:
final int A;
Constructor ()
{
A = 5;
}一种是用一个instance initializer:
final int A;
{
A = 5;
}方法中的常量只要在使用前赋值就可以了。
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);
}
}