简体   繁体   中英

Java - Static and Final variables

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.

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.

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();
    }
}

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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM