Why is it mandatory to initialize a final variable when an instance is built? [duplicate]

-1

In a review for certification, I came across the following code:

public class Initializer {
    static int si = 10;
    int i;
    final boolean bool; 
}

It generates compilation error in the declaration of variable final boolean bool , since it was not initialized. Note that the variable int i has not been initialized either, but it does not have any problem since the (non-final?) Instance variables are implicitly initialized.

But why, why is this differentiation with final and is it mandatory to initialize a final variable when an instance is built?

    
asked by anonymous 11.04.2017 / 05:01

1 answer

0

In a simple way the final modifier in variables serves to make it immutable, so in order for it to be immutable it must have its value initialized in the statement in which it is declared or in the class constructor.

In java all variables are initialized with their default values even if it is null, so if the final variable were initialized with a default value it would be useless because it is immutable and can not have its value modified. >     

11.04.2017 / 12:03