Jagadish Dabbiru
Jagadish Dabbiru

Reputation: 940

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

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.

Upvotes: 7

Views: 3942

Answers (4)

Nathan Hughes
Nathan Hughes

Reputation: 96385

For why the language works this way, that's because the favored style is to delay the declaration of local variables until they're first used. Java was designed so that you don't have to declare a variable until the spot where it is first used (unlike, for example, JavaScript, where due to lack of block-level scope and also due to hoisting you're better off declaring the variables at the start of the method or function), so it shouldn't be an issue that local variables don't get a default value.

Also for local variables the idea must have been that there was relatively little value in assigning a default value. Even if I do want to initialize a local variable it's good to be explicit about it.

Of course for instance and class members declare-at-first-use doesn't apply, because there's no telling what could call it. If something isn't assigned it needs to receive a default value.

Upvotes: 0

Makoto
Makoto

Reputation: 106410

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.

Upvotes: 2

Mik378
Mik378

Reputation: 22171

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.

Upvotes: 2

Kescha Skywalker
Kescha Skywalker

Reputation: 489

In the first case java can't know, when you assign the value to the node "head". You could have a call before calling "add" In the second the commands are in the same method and the order of execution is clear.

In every case you should initialize every variable. If I remember right Member variables are initialized to null, but I'm not sure it work out for every jvm

Upvotes: 0

Related Questions