Default Values and Initialization in Java

2019-01-04 18:09发布

Based on my reference, primitive types have default values and Objects are null. I tested a piece of code.

public class Main {
    public static void main(String[] args) {
        int a;
        System.out.println(a);
    }
}

The line System.out.println(a); will be an error pointing at the variable a that says variable a might not have been initialized whereas in the given reference, integer will have 0 as a default value. However, with the given code below, it will actually print 0.

public class Main {
    static int a;
    public static void main(String[] args) {
        System.out.println(a);
    }
}

What could possibly go wrong with the first code? Does class instance variable behaves different from local variables?

8条回答
Ridiculous、
2楼-- · 2019-01-04 18:21

All member variable have to load into heap so they have to initialized with default values when an instance of class is created. In case of local variables, they don't get loaded into heap they are stored in stack until they are being used before java 7, so we need to explicitly initialize them.

查看更多
相关推荐>>
3楼-- · 2019-01-04 18:23

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 default value .

查看更多
别忘想泡老子
4楼-- · 2019-01-04 18:24

In java the default initialization is applicable for only instance variable of class member it isn't applicable for local variables.

查看更多
爷的心禁止访问
5楼-- · 2019-01-04 18:26

yes, instance variable will be initialized to default value, for local variable you need to initialize before use

public class Main {
      int instaceVariable; // Instance variable will be initalized to default value
    public static void main(String[] args) {
        int localVariable = 0; // Local Variable Need to initalize before use
    }
}
查看更多
SAY GOODBYE
6楼-- · 2019-01-04 18:29

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 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 usage
        m = 20;
        n = new Object();
        o = new int[20]; //elements of o are initialized to 0
    }
}
查看更多
冷血范
7楼-- · 2019-01-04 18:29

Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.

There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.

If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.

查看更多
登录 后发表回答