11

我知道一旦为最终变量分配了一个值,就无法更改它。但是,我对此有几个问题:

  • 当我有一个字段时,比如说static final JButton button;在一个类之外,然后在main方法中,尝试为它分配一个值,button = new JButton("OK");我得到一个错误,告诉我删除 final 修饰符?但是,由于原始button变量尚未引用对象,我的印象是我可以分配一次?

  • 其次,如果我完全删除了对类外的引用,buttonstatic final JButton button;的 IDE 会声称“空白的最终字段按钮可能尚未初始化”。这是否意味着必须初始化所有最终字段?如果是这样,它们必须在那里初始化,然后我似乎无法在以后初始化它。

  • 另外,愚蠢的问题,但我最初的假设是,当最终变量被引用到实例或数据类型时,它不能分配给其他任何东西是正确的,对吧?

此代码不完整,但用于说明我的观点:

public class FinalVarTester {

    static final JButton button;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());
        button = new JButton("OK");
        container.add(button, BorderLayout.SOUTH);
    }

}
4

7 回答 7

18

您应该在静态初始化程序中或直接初始化静态最终变量。所以要么

static final JButton button = new JButton();

或者

static final JButton button;

static {
  button = new JButton();
}

The Java language specification has some more documentation about it: the section about final variables specifies why you get the compile error:

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

and chapter 16 talks about the definite assignment

于 2012-04-21T14:04:18.880 回答
3

final确实必须初始化字段,因为这将是程序其余部分的值。

一个例外是final可以在构造函数中初始化变量。由于静态字段不属于实例,它们必须被初始化(没有构造函数)直接初始化,或者静态初始化块是这样做的选项。

关于你的最后一个问题,是的,这就是为什么它被称为final。

于 2012-04-21T14:03:34.917 回答
0

类范围内的最终变量必须在声明或构造函数中初始化。除非在主函数中声明,否则不能在主函数中分配最终变量。所有最终字段都必须初始化。所有变量都必须在使用前进行初始化。

于 2012-04-21T14:02:46.587 回答
0

The problem is with initiallizing the final variable in a method inside the class(main method is method too :) ). In java "final" is like constant and it could be initiallized just once. Do it with composition instead of aggregation.

public class FinalVarTester {

    static final JButton button = new JButton("OK");

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());
        container.add(button, BorderLayout.SOUTH);
    }
}
于 2012-04-21T14:13:29.513 回答
0

The correct answer is that a static final var is always initialized at class init time -- either to the value you supply or to the default value (null, 0, false).

于 2012-04-21T14:32:50.660 回答
0

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable.

Types of Final Variables

1) INSTANCE FINAL VARIABLE:

A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise, a compile-time error occurs

2) STATIC FINAL VARIABLE

A blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs

In both the cases the intention is to avoid using the final variable before it is initialized.

So in your case it must be initialized via the static initializer block which initializes when the class is loaded.

Ref : http://en.wikipedia.org/wiki/Final_(Java)

于 2013-08-15T04:47:53.193 回答
0

A better nice codding way :

public class FinalVarTester 
{    
    static final JButton button; 
    public FinalVarTester()
    {    
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());
        container.add(button, BorderLayout.SOUTH);
        button = new JButton("OK");
    }
    public static void main(String[] args)    
    {
        FinalVarTester vTester = new FinalVarTester();    
    }
}
于 2017-04-16T18:08:45.670 回答