1

Is there any way in Java to create variables like final that are not initialized inside the constructor, but yet once they are initialized they can never be changed again? My problem is that I get the variable values at different time points and I'd like to create the class before or as soon as I receive the first value.

I've already thought about the obvious solution of keeping a flag for each variable, but I wanted to know if there's anything more efficient than that.

2 Answers2

0

I would probably do something along the lines of the last answer here. Always set the fields with a setter, and if the field is not the default value (i.e. null), do not allow it to be set.

Community
  • 1
  • 1
isaach1000
  • 1,819
  • 1
  • 13
  • 18
  • That's quite a good idea. But is it safe to assume that the variables will have the expected initial value or should I initialize them first on the constructor and use this value as the default? –  May 06 '13 at 09:10
  • "Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style." See this [article](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). – isaach1000 May 06 '13 at 13:36
  • Yes I've already read that. And basically the reason I asked the question was that sentence underlying that it "is generally considered bad programming style". –  May 06 '13 at 22:30
  • I believe that it is bad programming style to assume that a field is set to a given value that you plan on using. For example, one should not assume an int is 0 without setting it (especially because it is unclear when reading the code). But the only reason you check the default is to make sure you have not set a value yet, so there should not be a problem. – isaach1000 May 06 '13 at 23:13
  • Yes that sounds reasonable. Thanks for the help! :-) –  May 07 '13 at 10:04
0

While initializing this final variable make sure that you are initializing only in one of the following constructs, otherwise compiler throw an error.

  1. Initializer expression

    public class FinalVariable { // in the instance initializer expression, or while declaration itself // final = ; final int finalInstanceField = 5;

    }

  2. Instance initializer block

    public class FinalVariable { final int finalInstanceField;

    {
        // Initialization in instance initializer block
        finalInstanceField = 5;
    }
    
  3. Constructor block public class FinalVariable {

    final int finalInstanceField ;
    
    public FinalVariable() {
        // constructor
        finalInstanceField = 7;
    }
    

    }

Static final variables can be initialized in two ways. 1.Initializer expression

public class FinalVariable {
    // in the instance initializer expression, or while declaration itself
    // final <type> <variable_name> = <initializer expression>;
    static final int finalStaticField = 25;

}
  1. Static initializer block

    public class FinalVariable {

    static final int finalStaticField;
    
    static {
        finalStaticField = 7;
    }
    

    }

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136