Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java initializing only class variables by default but not local variables?

Tags:

java

oop

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?

enter image description here

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.

like image 529
Jagadish Dabbiru Avatar asked Oct 26 '14 18:10

Jagadish Dabbiru


People also ask

Why local variables are not initialized in Java?

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.

Are local variables initialized by default in Java?

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.

Why does Java compiler forces you to initialize local variables before using them but not for member variables?

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.

Are local variables initialized by default?

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.


2 Answers

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
  • etc.

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.

like image 194
Mik378 Avatar answered Oct 18 '22 22:10

Mik378


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.

like image 35
Makoto Avatar answered Oct 18 '22 22:10

Makoto