Java - final variables

前端 未结 7 1166
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 01:43

I know that once a final variable has a value assigned to it, it cannot be changed. However I just have a couple of questions regarding this:

  • When I have a

相关标签:
7条回答
  • 2020-12-16 01:59

    You should initialize a static final variable either in a static initializer, or directly. So either

    static final JButton button = new JButton();
    

    or

    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

    0 讨论(0)
  • 2020-12-16 01:59

    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)

    0 讨论(0)
  • 2020-12-16 02:06

    final fields must be initialized indeed, as this will be their value for the rest of the program.

    An exception to that is that a final variable may be initialized in the constructor. Since static fields don't belong to an instance they must be initialized (there is no constructor) direct initialization, or static initialization blocks are options for doing that.

    Regarding your last question, yes, this is why it's called final.

    0 讨论(0)
  • 2020-12-16 02:06

    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).

    0 讨论(0)
  • 2020-12-16 02:12

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 02:16

    Final variables in class scope must be initialized within the declaration or in the constructor. You cannot assign to a final variable in the main function unless it was declared in the main function. All final fields must be initialized. All variables must be initialized before they are used.

    0 讨论(0)
提交回复
热议问题