How to initialize a constant variable

0

I want to initialize a constant variable, which is'final', but I also want to include it in the constructor of a class but I do not know if that's fine:

public class Avion
{
    private final BigDecimal limiteAltura;
    public Avion(String limiteAltura)
    {
        this.limiteAltura = new BigDecimal(limiteAltura);
    }
}

It is not assumed that the constructor initializes a field with suitable values, well I initialize it with a String that by default will be null , or so I believe, but I get the impression that it can be done in another way. Also a constant has to be initialized at the time of its declaration or in the constructor so I do not know how to link all of this. What I want to achieve is to be explicit not to forget code.

    
asked by Anónimo 10.06.2017 в 11:43
source

1 answer

0
private final BigDecimal limiteAltura;

No, you do not initialize it here, you declare it here. It has the default value null , but you have not initialized it.

Your code is correct; while you only initialize it once (when declaring it or inside each one of the constructors) there is no problem.

    
answered by 10.06.2017 / 12:21
source