Answer the question
In order to leave comments, you need to log in
Java. What is the value of the variable after the declaration?
static int staticVar;
void someMethod() {
int locVar;
// мое мнение таково
// и в staticVar и в locVar
// значения 0
}
Answer the question
In order to leave comments, you need to log in
In the case of a static variable, it will be 0. In the case of the second, there will be a compilation error when accessing this variable. As you have written locVar will not be formally defined at all.
Once upon a time, someone decided that an uninitialized local variable is a probable bug in the program. And they were forbidden to use them without explicit initialization.
All default values of fields are explicitly written in the spec (for int it is 0).
If my memory serves me, then only the fields of the class (heap) are initialized to zero, but not the variables in the method (stack). Those. in the example above, the static variable will be initialized to zero, unlike the local variable, because static variable - the essence is a field in a class instance.
Well, it is clear that it is impossible to find out the value of a local variable without initialization - the compiler will not miss it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question