Initialization of local variable in a conditional block in Java
Last Updated :
11 Sep, 2018
Java comprises 5 conditional blocks namely - if, switch, while, for and try.
In all these blocks, if the specified condition is true, the code inside the block is executed and vice-versa. Also, Java compiler doesn't let you leave a local variable uninitialized.
While initializing local variable inside a conditional block, one must bear the following 3 details in mind:
1. If the specified condition is true, and 'values' are provided in the condition, the program compiles fine.
2. If the specified condition is true, but 'variables' are provided in the condition, we get a compilation error.
3. If the specified condition is false, we get a compilation error.
The above-given points are true for both primitive and reference type local variables.
Example:
The code below will give a compilation error.
Java
// Java program to demonstrate error if we
// assign value to an uninitialized variable
// only in if block.
public class InitTesting {
public static void main(String args[])
{
int i = 100;
int j;
// Note that the condition is false
if (i > 500)
j = i + 5;
System.out.println("j :" + j);
}
}
Output:
prog.java:8: error: variable j might not have been initialized
System.out.println("j :" + j);
^
1 error
The condition in the if statement is false. Hence, local variable 'j' never gets initialized. Thus, trying to reference uninitialized variable 'j' in line8 gives a compilation error.
To avoid this, initialize your local variable to a default value outside the conditional block.
The code below works fine-
Java
// Java program to demonstrate that the above
// error is gone if we initialize the variable.
public class InitTesting {
public static void main(String args[])
{
int i = 100;
int j = 0;
// Note that the condition is false
if (i > 500) {
j = i + 5;
}
System.out.println("j :" + j);
}
}
Output:
j :0
Also, in Java, 'values' are read at compile time. But, 'variables' are read at run-time. Hence, when variables are a part of the condition, and another variable is initialized inside the conditional block, it gives an unexpected compile-time error.
Example: Have a look at the code below:
Java
// Java program to demonstrate error even if
// condition is true.
class Test {
public static void main(String args[])
{
int a = 90;
int b = 80;
int i;
// The condition is true
if (a > b) {
i = a + 5;
}
System.out.println("i :" + i);
}
}
Output:
prog.java:9: error: variable i might not have been initialized
System.out.println("i :" + i);
^
1 error
It gives a compilation error irrespective of the condition being true or false because Java didn't read variables at compile time and thus 'i' isn't initialized.
On the other hand, this doesn't happen if values are specified instead of variables.
Java
// Java program to demonstrate that there is
// no error if we constants in if condition.
class Test {
public static void main(String args[])
{
int i;
if (90 > 80) {
i = a + 5;
}
System.out.println("i :" + i);
}
}
Output:
i :95
Similar Reads
Order of execution of Initialization blocks and Constructors in Java In Java, there are various techniques, which we can use to initialize and perform operations on objects such as methods, constructors, and initialization blocks. These tools are used to ensure that the program works as expected. Instance Initialization Blocks (IIB) are used to initialize instance va
4 min read
Instance Initialization Block (IIB) in Java In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIBs are used to initialize instance variables. So firstly, the constructor is invoked and the java compiler copies the instance initializer block in the constructor
3 min read
Instance Variable as Final in Java As we all know, when the value of a variable varies from object to object, then that type of variable is known as an instance variable. The instance variable is declared inside a class but not within any method, constructor, or block.If we don't initialize an instance variable, then the JVM automati
3 min read
Final Local Variables in Java In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u
3 min read
How to Execute Instance Initialization Block (IIB) without Creating Object in Java? In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIB are used to initialize instance variables. We know that the instance block is the name-less method in java inside which we can define logic and they possess certa
3 min read
Double Brace Initialization in Java The combination of two separate processes in Java is known as Double Brace Initialization in Java. As the name suggests, there are two braces {{ included in it. A single brace { is nothing new for programmers. The first brace in the double brace initialization is used to create an anonymous inner cl
4 min read