1

I'm aware this won't work but it's just an example so I can understand why it won't work.

public class MyClass {

    final static JButton b;

 public void myMethod() {
   b = new JButton("Hello!");
 }
 }

1) Why is there an error for the final static JButton b; line? And why does this error disappear when I remove the static keyword?

2) With or without the static keyword, there is an error for the line b = new JButton("Hello!"); I'm wondering why this is? I thought it would be okay to have a final variable instantiated within a method.

0

5 Answers 5

7

When you declare a static field to be final, it must be initialized statically, either in a static initializer block or with an initializer expression. You cannot initialize it in a constructor.

When you declare a non-static field to be final, it must be initialized in every constructor, or with an initializer expression, or in an instance initializer block.

Sign up to request clarification or add additional context in comments.

2 Comments

Just want to clarify for asker, the reason you can't initialize a static final in the constructor is because accessing static properties doesn't require construction of the object.
@ametren Not quite -- because if it's not final, you can set it in a constructor (even though it still doesn't require construction of the object). The reason you can't set it in a constructor is that a static final can only be assigned once, whereas a constructor can be invoked many times.
1

Since the field is static final it needs to be initialized with the class itself, the compiler is requesting that you provide a value other than the default one. You can either provide a default initializer or provide a value in a initialization block:

Either

public class MyClass {
  final static JButton b = new JButton("Hello!");
}

or

public class MyClass {
  final static JButton b;

  static{
      b = new JButton("Hello!");
  }
}

are valid.

By the time yourMethod is invoked it would be already too late, because it is expected that at that point your class is already initialized, and you would expect the field in question to have been initialized as well.

Comments

1

Some people should reread what static and final truely is..

A static variable is global for every instance of its class. If you have a class Person, with an "int static age = 0" in it and you say age++; in your constructor, age will have in every instance the same value! Means 3x new Person() expects age to be 3, in EVERY Person instance! michael.age == 3 and susan.age == 3 and tom.age == 3

Therefor you need to reserve some space for that variable and have to initialize AND declare it! Because this means "you have ONE variable for all of your instances of a class"...

next, a final variable has to be filled with data on its declaration, because you MAY NOT! edit that variable later! Like a const (constant)

so there is last static final. As you might have learned, this variable is for every instance of a class, initialized on its declaration and is is not changeable overall! Which explains the class comprehensive "const" (constant) variable for java!

There you go!

Comments

1

With final assignment is only allowed immediately or in the constructor. So only the following is allowed.

class A {
    static final B b = new B();
    static final C c;
    static { // Static initializer block.
         c = new C();
    }
    final B b2 = new B();
    final C c2;
    { // Initializer block.
         c2 = new C();
    }
    final D d2;
    A() {
        d2 = new D();
    }
}

4 Comments

You could also initialize a non-static final field in an instance initializer block.
@Fly didn't I do that for c2? Maybe my "anonymous constructor" misled you.
Doh, I didn't see that somehow. It could be the use of the word "constructor" led me astray.
What you labeled an "anonymous constructor" is actually an instance initializer block. An anonymous constructor is something completely different. (See Java Language Specification, §15.9.5.1.) In particular, you can have both a constructor and an instance initializer block. As described in JLS §12.5, the instance initializer block executes shortly after the constructor starts executing.
0

A static final variable must be initialized directly (near to the variable) or by using a static block.

A final variable (non-static) must be declared either directly (near to the variable) or by using an initialization block, or in the object constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.