IDontKnow006
IDontKnow006

Reputation: 15

Uninitialized local variable - No error : Java

Here are two uninitialized local variables. Still this does not give compile time or runtime error and executes completely. Is this thing permissible in Java and How (explanation will be welcome).

class A2{ }

public class A {
public static void main(String[] args) {
    int x;
    A2 a2;

    System.out.println("Main");
}

}

Upvotes: 1

Views: 2902

Answers (4)

Fatih
Fatih

Reputation: 1

you will get a compile-time error once you start using uninitialized local variables. For example:

Case 1: No error

public static void doJob(int[] a){
    
   int temp;   
}

Case 2: Gives error

 public static void doJob(int[] a){
    
    int temp;
    System.out.println(temp);    
}

Upvotes: 0

Dinesh Peiris
Dinesh Peiris

Reputation: 56

Yes, local variable declarations you have done in the above code is allowed as long as you don't access them. If you happen to write any code that access those variables, the code will not compile.

According to Java Language Specification, you can't access local variables (the variables that you declare inside a method) unless they are initialized before accessing. Below is from Java Language Specification for SE 8.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.4.1

Chapter 16 - Definite Assignment

"For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs."

Chapter 14

14.4.2 Execution of Local Variable Declarations A local variable declaration statement is an executable statement. Every time it is executed, the declarators are processed in order from left to right. If a declarator has an initializer, the initializer is evaluated and its value is assigned to the variable. If a declarator does not have an initializer, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs by the rules of §16 (Definite Assignment). Each initializer (except the first) is evaluated only if evaluation of the preceding initializer completes normally. Execution of the local variable declaration completes normally only if evaluation of the last initializer completes normally. If the local variable declaration contains no initializers, then executing it always completes normally.

Upvotes: 0

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

There's nothing incorrect about that code. You don't actually use those variables so there's no issue. If you did try to use them it would then become a problem. For example,

System.out.println(a2);
System.out.println(x);

would bring about "Variable 'x'/'a2' might not have been intitialized" errors. There will be no default values or ability to run the code. It will be a compile time error and your code won't run. If the variables were class fields they would get default values for certain types or null otherwise.

Upvotes: 3

Shridevi
Shridevi

Reputation: 19

Both variables are not used in code. Once you try to use it as System.out.println("Main" + x); It will give you compilation error as local variable is not initialized.

Upvotes: 0

Related Questions