What happens to a declared, uninitialized variable in Java?
Solution 1
How exactly a JVM does this is entirely up to the JVM and shouldn't matter for a programmer, since the compiler ensures that you do not read uninitialized local variables.
Fields however are different. They need not be assigned before reading them (unless they are final) and the value of a field that has not been assigned is null for reference types or the 0 value of the appropriate primitive type, if the field has a primitive type.
Using s.isEmpty() for a field String s; that has not been assigned results in a NullPointerException.
So as I see now, it makes a big difference if the variable is declared
locallyor in theClass, though I seem to be unable to understand why when declaring in the class it gives no error, but when declaring in the main it produces the"Not Initialized"error.
In general it's undesirable to work with values that do not have a value. For this reason the language designers had 2 choices:
a) define a default value for variables not yet initialized
b) prevent the programmers from accessing the variable before writing to them.
b) is hard to achieve for fields and therefore option a) was chosen for fields. (There could be multiple methods reading/writing that could be valid or invalid depending on the order of calls, which could only be determined at runtime).
For local variables option b) is viable, since all possible paths of the execution of the method can be checked for assignment statements. This option was chosen during the language design for local variables, since it can help to find many easy mistakes.
Solution 2
Fabian already provided a very clear answer, I just try to add the specification from the official documentation for reference.
Fields in Class
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
If not specified the default value, it only be treated as a bad style, while it's not the same case in local variables.
Local Variables
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
Solution 3
The default value will be based on the type of the data and place where you are using initialized variable . Please refer below for Primitive default.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Sir. Hedgehog
System.out.println("Hello World"); What else to say... Java/EE is my passion and I follow it. Also long time pc gamer, love Rust, CS:GO, AOE III, Escape From Tarkov and more. Current aim, become a Senior Java Developer. I am a really nice person, but if you step on my foot I will rage you till the end of time :D It should go without saying that the opinions stated by me are my own and not necessarily those of my employers past, current and future.
Updated on July 17, 2020Comments
-
Sir. Hedgehog over 2 yearsDoes it have a value?
I am trying to understand what is the state of a declared but not-initialized variable/object in Java.
I cannot actually test it, because I keep getting the "Not Initialized" compile-error and I cannot seem to be able to suppress it.
Though for example, I would guess that if the variable would be an
integerit could be equal to 0.But what if the variable would be a String, would be it be equal to
nullor theisEmpty()would returntrue?Is the value the same for all non-initialized variables? or every declaration (meaning, int, string, double etc) has a different value when not explicitly initialized?
UPDATE
So as I see now, it makes a big difference if the variable is declared
locallyor in theClass, though I seem to be unable to understand why when declaring as static in the class it gives no error, but when declaring in the main it produces the"Not Initialized" error. -
Sir. Hedgehog over 6 yearsi understand that now, could you also see the update and tell me about that as well? -
SomeJavaGuy over 6 years@hedgehog the same answer, declaring it as a static variable on class level vs delaring it locally in a static method