Default values and initialization in Java Default values and initialization in Java java java

Default values and initialization in Java


In the first code sample, a is a main method local variable. Method local variables need to be initialized before using them.

In the second code sample, a is class member variable, hence it will be initialized to the default value.


Read your reference more carefully:

Default Values

It's not always necessary to assign a value when a field is declared.Fields that are declared but not initialized will be set to areasonable default by the compiler. Generally speaking, this defaultwill be zero or null, depending on the data type. Relying on suchdefault values, however, is generally considered bad programmingstyle.

The following chart summarizes the default values for the above datatypes.

. . .

Local variables are slightly different; the compiler never assigns adefault value to an uninitialized local variable. If you cannotinitialize your local variable where it is declared, make sure toassign it a value before you attempt to use it. Accessing anuninitialized local variable will result in a compile-time error.


These are the main factors involved:

  1. member variable (default OK)
  2. static variable (default OK)
  3. final member variable (not initialized, must set on constructor)
  4. final static variable (not initialized, must set on a static block {})
  5. local variable (not initialized)

Note 1: you must initialize final member variables on every implemented constructor!

Note 2: you must initialize final member variables inside the block of the constructor itself, not calling another method that initializes them. For instance, this is not valid:

private final int memberVar;public Foo() {    // Invalid initialization of a final member    init();}private void init() {    memberVar = 10;}

Note 3: arrays are Objects in Java, even if they store primitives.

Note 4: when you initialize an array, all of its items are set to default, independently of being a member or a local array.

I am attaching a code example, presenting the aforementioned cases:

public class Foo {    // Static and member variables are initialized to default values    // Primitives    private int a; // Default 0    private static int b; // Default 0    // Objects    private Object c; // Default NULL    private static Object d; // Default NULL    // Arrays (note: they are objects too, even if they store primitives)    private int[] e; // Default NULL    private static int[] f; // Default NULL    // What if declared as final?    // Primitives    private final int g; // Not initialized. MUST set in the constructor    private final static int h; // Not initialized. MUST set in a static {}    // Objects    private final Object i; // Not initialized. MUST set in constructor    private final static Object j; // Not initialized. MUST set in a static {}    // Arrays    private final int[] k; // Not initialized. MUST set in constructor    private final static int[] l; // Not initialized. MUST set in a static {}    // Initialize final statics    static {        h = 5;        j = new Object();        l = new int[5]; // Elements of l are initialized to 0    }    // Initialize final member variables    public Foo() {        g = 10;        i = new Object();        k = new int[10]; // Elements of k are initialized to 0    }    // A second example constructor    // You have to initialize final member variables to every constructor!    public Foo(boolean aBoolean) {        g = 15;        i = new Object();        k = new int[15]; // Elements of k are initialized to 0    }    public static void main(String[] args) {        // Local variables are not initialized        int m; // Not initialized        Object n; // Not initialized        int[] o; // Not initialized        // We must initialize them before use        m = 20;        n = new Object();        o = new int[20]; // Elements of o are initialized to 0    }}