Initialize a static final field in the constructor

public class A
{
private static final int x;


public A()
{
x = 5;
}
}
  • final means the variable can only be assigned once (in the constructor).
  • static means it's a class instance.

I can't see why this is prohibited. Where do those keywords interfere with each other?

104931 次浏览

static final variables are initialized when the class is loaded. The constructor may be called much later, or not at all. Also, the constructor will be called multiple times (with each new object ), so the field could no longer be final.

If you need custom logic to initialize your static final field, put that in a static block

A constructor will be called each time an instance of the class is created. Thus, the above code means that the value of x will be re-initialized each time an instance is created. But because the variable is declared final (and static), you can only do this

class A {
private static final int x;


static {
x = 5;
}
}

But, if you remove static, you are allowed to do this:

class A {
private final int x;


public A() {
x = 5;
}
}

OR this:

class A {
private final int x;


{
x = 5;
}
}

Think about what happens the second time you instantiate an object. It tries to set it AGAIN, which is expressly prohibited by being a static final. It can only be set one time for the entire class, not instance.

You should set the value when you declare it

private static final x=5;

If you need additional logic, or more complex instantiation, this can be done in a static initializer block.

Think about it. You could do this with your code:

A a = new A();
A b = new A(); // Wrong... x is already initialised

The correct ways to initialise x are:

public class A
{
private static final int x = 5;
}

or

public class A
{
private static final int x;


static
{
x = 5;
}
}

static means that the variable is unique on the application. final means that it should be set only once.

If you set it in your constructor, you allow to set the variable more than once.

Hence you should intialize it directly or propose a static method to initialize it.

Final doesn't mean that is has to be initialized in the constructor. Generally this is what is done :

 private static final int x = 5;

static instead means that the variable will be shared through multiple instances of the class. For example :

public class Car {
static String name;
public Car(String name) {
this.name = name;
}
}


...


Car a = new Car("Volkswagen");
System.out.println(a.name); // Produces Volkswagen


Car b = new Car("Mercedes");
System.out.println(b.name); // Produces Mercedes
System.out.println(a.name); // Produces Mercedes
    public class StaticFinalExample {
/*
* Static final fields should be initialized either in
* static blocks or at the time of declaration only
* Reason : They variables are like the utility fields which should be accessible
* before object creation only once.
*/
static final int x;


/*
* Final variables shuould be initialized either at the time of declaration or
* in initialization block or constructor only as they are not accessible in static block
*/
final int y;


/*
* Static variables can be initialized either at the time of declaration or
* in initialization or constructor or static block. Since the default value is given to the
* static variables by compiler, so it depends on when you need the value
* depending on that you can initialize the variable appropriately
* An example of this is shown below in the main method
*/
static int z;


static {
x = 20; // Correct
}
{
y = 40; // Correct
}
StaticFinalExample() {
z = 50; // Correct
}
public static void main (String args[]) {
System.out.println("Before Initialization in Constructor" + z);   // It will print 0
System.out.println("After Initializtion in Constructor" + new StaticFinalExample().z); // It will print 50
}
}

Static

The keyword static means that a member of an object, in this case a field, is not tied to an instance of a class, but is a member of the class instead. If the static member is a field, it is initialised during loading of a class.

It is accessible through the class rather than through an instance (though the latter is not impossible, it is considered bad form), so it is accessible without the constructor having ran at all — ever.

Final

The keyword final, when applied to a field of an object, means that it can be assigned to only once, and that it has to be assigned to during initialisation.

Static Final

Taken together, these two keywords effectively define a constant: it can be assigned to only once, has to be assigned to, and is the same for all instances of that class.

Since the static field is initialised during class loading, it has to be initialised then, either at declaration, or in a static initialiser block.

This means that if and when you reach the constructor, it will already have been initialised, because it needed to already have been initialised.

Singleton

If you're looking for a class member that you only assign to once, but read many times, you're dealing with a singleton. The singleton pattern is commonly used for access to a shared resource.

The field is made static but not final; instead when accessing the field, the code checks whether it has been initialised already, if not, it is done then and there. Note that in environments with multiple threads, you need to synchronise access to the field, to avoid accessing it while it is initialising.