[Fixed] Variable might not have been initialized in Java

In this post, we will learn how to solve variable might not have been initialized error in Java. The main cause of this error is that you have declared the variable but forgot to initialize them.

Read Also:  Types of Variables in Java

Variable declaration and variable initialization are two different things altogether. Let's understand them first:

a. Variable declaration
 int a,b;

b. Variable Initialization
 int a = 2;
int b = 3;

Producing the Error:

First, we will produce the error before moving on to the solution.
 public class Variable {
    public static void main(String args[]) {
      int var1 = 3;
      int var2;
      int sum = var1 + var2;
      System.out.println(sum);
    }
}

Output:
/Variable.java:5: error: variable var2 might not have been initialized
int sum = var1 + var2;
                             ^
1 error


In the above example, local variable var2 is declared but not initialized and we are trying to use its value, hence we are getting the error.

Solution: Variable might not have been initialized

1. Initialize the local variable


One way to solve the error is by initializing the local variable as shown below. 
 public class Variable2 {
    public static void main(String args[]) {
      int var1 = 3;
      int var2 = 10; //no error as var2 variable is initialized
      int sum = var1 + var2;
      System.out.println(sum);
    }
}

Output:
13

Note: This error will not occur if you just declare the local variable without using it in the code.

2. Declare a variable as a static variable or instance variable


Another way to get rid of this error is to declare the variable as a static variable or instance variable
 public class Variable3 {
    
    static int var2;// declare var2 as static variable
    public static void main(String args[]) {
      int var1 = 3;
      int sum = var1 + var2;
      System.out.println(sum);
    }
}

Output:
3

Note: If you do not initialize member variables e.g. static or instance(non-static) variable then you will not get this error because they are initialized with their default values as shown in the example below.


 public class Variable4 {
    public static int staticVar;
    private int instanceVar;
    public static void main(String args[]) {
      int var1 = staticVar;// no error because staticVar is static variable
      Variable obj = new Variable();
      int sum = obj.instanceVar; // no error because instanceVar is instance variable
      System.out.println(var1 + " "+ sum);
    }
}

Output:
0 0

Tricky Scenarios Where This Error Can Occur

1. Creating more than 1 local variable in the same line


Below, you might think that both variables "a" and "b" are initialized to zero but that's not the case here. Only variable "b" is initialized whereas variable "a" is not initialized. As a result, when you will run the program you will get a variable might not have been initialized error.
 public class Variable5 {
    public static void main(String args[]) {
      int a,b= 0;
      System.out.println("value of a: " + a);
      System.out.println("value of b: " + b);
    }
}

Output:
/Variable5.java:4: error: variable a might not have been initialized
System.out.println("value of a: " + a);
                                                            ^
1 error


2. Initialize the variable inside if block


One more tricky condition is when you initialize the variable inside if block, since it is a condition block, compiler complains about the error when if block is not executed as shown below in the example:
 public class Variable6 {
    public static void main(String args[]) {
      int sum;
      if(args.length > 0)
        sum = args.length;
      System.out.println("sum is: " + sum);
    }
}

Output:
/Variable6.java:6: error: variable sum might not have been initialized
System.out.println("sum is: " + sum);
                                                       ^
1 error


In the above scenario, the variable sum is not initialized before you use it on System.out.println() if args length is zero. Hence compiler throws variable might not have initialized error.

What will happen if we handle args length is zero as well in the code. Let's find out:
 public class Variable7 {
    public static void main(String args[]) {
      int sum;
      if(args.length > 0)
        sum = args.length;
      if(args.length == 0)
        sum = 10;
      System.out.println("sum is: " + sum);
    }
}

Output:
/Variable7.java:8: error: variable sum might not have been initialized
System.out.println("sum is: " + sum);
                                                       ^
1 error


In the above code, you might think that sum will always initialize because args length can either be zero or greater than zero. If you run the above program you will still get the variable might not have been initialized error.
One way to get rid of the error is by using else block as shown below:
 public class Variable8 {
    public static void main(String args[]) {
      int sum;
      if(args.length > 0)
        sum = args.length;
      else 
        sum = 10;
      System.out.println("sum is: " + sum);
    }
}

Output:
sum is: 10

That's all for today. Please mention in the comments in case you have any questions related to the error variable might not have been initialized.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry