1

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");
}

}
1
  • I have read "Always initialized a local variable" "A uninitialized local variable violates the java rules Commented Feb 26, 2016 at 6:13

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If in this code x is getting a default value , then can I print this value?
In this what value will they get. I am not using them. Would they hold some thing in stack when this code is written?
0

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.

4 Comments

In this what value will they get. I am not using them. Would they hold some thing in stack when this code is written?
@IDontKnow006 As you can never observe that value, the question is pointless. It's even possible that the variables are removed completely.
@EJP I didn't got your that statement - It's even possible ........ removed completely."
@IDontKnow006 You didn't get what about it? It seems clear to me. An unused local variable contributes nothing at runtime and is an obvious candidate for being 'optimized' away.
0

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.

Comments

0

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);    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.