13

Is there a way in Java to check the variable is initialized of not inside a class. I found for javascript but not for Java. Hear is an example of what I am looking for.

private float[] Average;
private void Check(){
if(/*variable is not initialized*/){ Average = new float[4];}
}
Community
  • 1
  • 1
raz
  • 482
  • 1
  • 5
  • 17
  • 3
    Usually the java compliler will warn you when you try to reference a non-initialized variable. – EJK Dec 27 '15 at 01:22

4 Answers4

17

Arrays in java works like objects (they are not primitive types).

So yes, you can check if your array was initialized or not with :

private void check(){
    if(average == null){
        average = new float[4];
    }
}



A better solution (if you know the array size when instantiate)

But in my opinion, you'd better initialize the variable in your class constructor, like bellow :

public class MyClass {
    private float[] average;

    public MyClass(int arraySize) {
        this.average = new float[arraySize];
    }
}

This way, you'll be sure it is initialized everytime a MyClass object is created.

An even better solution (even if you don't know the array size when instantiate)

If you don't know the size of the array, i'd better use a List :

public class MyClass {
    private List<Float> average;

    public MyClass() {
        this.average = new ArrayList<>();
    }
}

Lists are resized automatically as they goes full.

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
  • if I do not know what size of the array is going to be then I suppose It is not possible to initialize it inside the constructor.. right? – raz Dec 27 '15 at 01:29
  • I added examples that might help you, one with a fixed array size on initialization, and an example with a List instead of an array. – Anthony Raymond Dec 27 '15 at 01:35
14

You can use if (Average == null) to check if it's null, but you cannot tell if it was explicitly set to null or just null by default. This works for every Object type (arrays are also objects), because objects' default value is null. The 8 primitive types (int, byte, float, char, long, short, double and boolean) however cannot be null. E.g. an int is 0 by default if you do not assign a value to it.

Andrea Dusza
  • 2,080
  • 3
  • 18
  • 28
  • 2
    *"they always have a default value"* ... `null` is the default value of object types, so every variable is initialized, then the programm passed compilation. – Tom Dec 27 '15 at 01:30
6

If you are worried about the variable outside the class definition then you should check if there are null or not.

null will indicate that variable is not initialized. In case of primitive types, such as int, double and similar you will be notified by the compiler that particular variable is not initialized.

In classes fields get initialized with default values even before constructor execution.

hrust
  • 734
  • 1
  • 11
  • 34
3

You could declare the field as final. Doing that, you have to initialize it, either in the constructor or during variable declaration. Using a final field would make your variable-not-initialized check redundant:

private final float[] average = new float[SIZE_OF_ARRAY];

or

private final float[] average;

public MyClass(int sizeOfArray) {
    this.average = new float[sizeOfArray];
}
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30