Why does Java initialize only class variables by default, but not local variables?

I am learning a linked list in Java and I have three files Main.java , List.java and Node.java . When I do this, I have a question why I should initialize a local variable declared in a method, but not a class variable declared in a class.

In the first pic, I declared the head of a class variable, it did not produce any error.

But in the second pic, I initialized head as a local variable. Now it throws an error to initialize a local variable.

What is the difference if declared as a class variable?

enter image description here

New to Java.

Update: I know how to fix this, but I don’t understand why Java initializes only class variables by default, but not local ones.

+6
source share
4 answers

Static / Non-static fields that are not primitives, like your Node , are initialized to null by default. Static / non-static fields that are primitive get default values.

There is also another case where some variables are initialized by default: when creating an array instance. Each cell represents a default value of the type:

  • 0 for int
  • null for Integer
  • and etc.

However, in the local method, the compiler does not assign a default value to local variables.
This is why your IDE warns about: "cannot be initialized!".

To understand why, you may be interested in this post.

0
source

This is pretty well explained by the Java Language Specification (in particular, §4.12.5):

Each variable in the program must have a value before its value:

  • 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 set before using it by initialization (§14.4) or assignment (§15.26), in a way that can be verified using rules for a specific purpose (§16).

To expand the bit, §16 goes into the rules for a specific purpose , which is the reason for this:

Each local variable (§14.4) and each empty final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access to its value occurs.

For each access to a local variable or an empty final field x, it is imperative to assign compile-time errors before accessing or occur.

Simply put: Java will assign default values ​​to class / instance variables, but will not assign default values ​​to a local variable. Local variables must be definitely assigned this way (either by initialization or assignment), or a compilation error will occur (as you noticed).

If you think about it from a different angle, when you initialize a class containing certain fields, you may not want to initialize the value first (think JavaBeans ). If you are in a code block and you declare a variable, then instead it is expected that the developer will control this object life cycle right there, in the block.

It does not make sense to simply declare a variable and try to do something with it without assigning a value to it, since the variable does not matter.

+2
source

In the first case, java cannot know when you assign the value to node "head". You could call before calling add, The second command is in the same method, and the execution order is clear.

In each case, you must initialize each variable. If I remember that the correct member variables are initialized to zero, but I'm not sure if this works for every jvm

0
source

Why does the language work this way because the preferred style is to defer the declaration of local variables until they are first used. Java was designed in such a way that you do not need to declare a variable until it is used (unlike, for example, JavaScript, due to the lack of a block level, and also because of the rise, you better declare variables at the beginning of the method or functions), so it should not be a problem that local variables do not get default values.

Also for local variables, the idea was that when assigning a default value, there was relatively little value. Even if I want to initialize a local variable, it would be nice to be explicit.

Of course, for example, the members of the declare-at-first-use class do not apply, because they do not say what can be called. If something is not assigned, it needs to get a default value.

0
source

Source: https://habr.com/ru/post/977246/


All Articles