这么说我分两行初始化也应该可以的public final int users;
users = 10;在 class level 通不过
local level 可以
为什么?

解决方案 »

  1.   

    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的变量不必一定在定义的时候初始化
      

  2.   

    编译器强迫你一定得对所有final执行赋值动作---如果不是发生在其定义处,就德在每个钩爪函数中以表达式方式设定其值。还是为了保证final在使用前绝对会被初始化。因为钩爪函数肯定先得到执行。定义就是这样的。local level是在constructor里面吧???
      

  3.   

    local level 是在 一个普通的函数里>但是在使用之前一定要初始化
    可是即使我不使用它 也无法编译通过
      

  4.   

    可是为什么可以在构造函数里给它赋值呢?public final int users;
    声明在 class level 里不时会自动赋给初值 0 吗;
    既然是 final 怎么还能 在赋值呢?
      

  5.   

    blank final就是这么用的,原因也没研究过。我说得对不对的?普通函数里也可以赋值的么?怎么想也不应该,应该会出错的啊。
      

  6.   

    类中的常量必须手动赋值,如果你留着让系统自动赋一个初始值,那你怎么改?有三种方法:
    一种就在声明的时候:
     final int A = 5;一种是在构造函数中:
     final int A;
     Constructor ()
     {
        A = 5;
     }一种是用一个instance initializer:
     final int A;
     {
        A = 5;
     }方法中的常量只要在使用前赋值就可以了。
      

  7.   

    又查了一些材料总算是明白了
    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);
       }
    }