Search code examples
javainitializationprimitive-types

Java initialize variable with null or not?


I was wondering whether I should initialize class members in java with an initial value and then change that value to some other given value in the constructor, or should I avoid doing such a thing?

code example

public class Test {
    private int value = 5;

    public Test(int value) {
        this.value = value;
 }
}

Solution

  • If not specified,:

    primitive bytes, shorts, ints, longs, floats and doubles are initialized to 0

    booleans are initialized to false

    Objects are initialized to null