I am learning linkedlist in Java and I have three files Main.java
, List.java
and Node.java
. When I am doing this, I got a question why should I initialize a local variable which is declared in method and but not class variable which is declared in class.
In the first pic, I declared head as class variable, it doesn't throw any error.
But in the second pic I initialized head as local variable.Now, it throws a error to initialize local variable.
What makes the difference when declared as class variable?
Beginner in Java.
Update: I know how to fix this but I am not clear why Java initializing only class variables by default but not local variables.
The local variables are stored on a stack, but instance variables are stored on the heap, so there are some chances that a previous value on the stack will be read instead of a default value as happens in the heap. For that reason the JVM doesn't allow to use a local variable without initializing it.
No, local variables do not have default values. Once we create a local variable we must initialize it before using it. Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value.
Local variables must be initialized before use, as they don't have a default value and the compiler won't let us use an uninitialized value.
The local variable name has been used, that is, read from, before it has been assigned a value. In C and C++, local variables aren't initialized by default.
Static/Non-static fields that are not primitives, like your Node
, are initialized at null
by default.
Static/Non-static fields that are primitive gets their default values.
There's also another case where some variables are initialized with default: when you instantiate an array. Each cell represents has default value, regarding the type:
0
for int
null
for Integer
However, in a local method, compiler does not assign default value to local variables.
That's why your IDE warns about: "may not be initialized!".
To understand why, you may be interested in this post.
This is explained rather well by the Java Language Specification (specifically, §4.12.5):
Every variable in a program must have a value before its value is used:
- Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
- For all reference types (§4.3), the default value is null.
- A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).
To expand a bit, §16 goes into the rules for definite assignment, which is at the crux of the reason:
Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.
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.
Simply put: Java will assign default values to class/instance variables, but will not assign a default value to a local variable. Local variables must be definitely assigned in this manner (either by initialization or assignment), or a compile time error will occur (as you observe).
If you think of it from another angle, when you initialize a class that contains specific fields, you may not want those initialized to a value at first (think JavaBeans). If you're in a code block and you declare a variable, the expectation instead is on the developer to control that object's life cycle right there in the block.
It doesn't make sense to simply declare a variable and attempt to do something with it without assigning a value to it, as the variable doesn't have a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With