Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
int x;
System.out.println(x);
}
}It does not compile
- Expecting
0: a local variable gets no default value, and javac rejects readingxbefore it is assigned. - Expecting a failure at run time: javac checks that a variable is assigned before every read, so the program is never built.
- Why does a
staticfield get a default value while a local variable does not? - How does javac decide that a variable is definitely assigned after an
if?