Why are local variables not initialized in Java?
Asked Answered
D

16

114

Was there any reason why the designers of Java felt that local variables should not be given a default value? Seriously, if instance variables can be given a default value, then why can't we do the same for local variables?

And it also leads to problems as explained in this comment to a blog post:

Well this rule is most frustrating when trying to close a resource in a finally block. If I instantiate the resource inside a try, but try to close it within the finally, I get this error. If I move the instantiation outside the try, I get another error stating that a it must be within a try.

Very frustrating.

Disloyalty answered 6/1, 2009 at 7:3 Comment(5)
Same question: https://mcmap.net/q/195622/-uninitialized-variables-and-members-in-javaAili
Sorry about that... this question didn't popup when I was typing in the question.. however, I guess there's a difference between the two questions... I want to know why the designers of Java designed it this way, whereas the question you pointed to does not ask that...Disloyalty
See also this related C# question: #1543324Grapheme
Simply - because it's easy for the compiler to track uninitialized local variables. If it could to the same with other variables, it would. The compiler is just trying to help you.Slushy
Similar question for C# (despite the title - see the answers): Are C# uninitialized variables dangerous?Schmeltzer
R
67

Local variables are declared mostly to do some calculation. So it's the programmer's decision to set the value of the variable and it should not take a default value.

If the programmer, by mistake, did not initialize a local variable and it takes a default value, then the output could be some unexpected value. So in case of local variables, the compiler will ask the programmer to initialize it with some value before they access the variable to avoid the usage of undefined values.

Recalcitrate answered 6/1, 2009 at 7:18 Comment(0)
S
24

The "problem" you link to seems to be describing this situation:

SomeObject so;
try {
  // Do some work here ...
  so = new SomeObject();
  so.DoUsefulThings();
} finally {
  so.CleanUp(); // Compiler error here
}

The commenter's complaint is that the compiler balks at the line in the finally section, claiming that so might be uninitialized. The comment then mentions another way of writing the code, probably something like this:

// Do some work here ...
SomeObject so = new SomeObject();
try {
  so.DoUsefulThings();
} finally {
  so.CleanUp();
}

The commenter is unhappy with that solution because the compiler then says that the code "must be within a try." I guess that means some of the code may raise an exception that isn't handled anymore. I'm not sure. Neither version of my code handles any exceptions, so anything exception-related in the first version should work the same in the second.

Anyway, this second version of code is the correct way to write it. In the first version, the compiler's error message was correct. The so variable might be uninitialized. In particular, if the SomeObject constructor fails, so will not be initialized, and so it will be an error to attempt to call so.CleanUp. Always enter the try section after you have acquired the resource that the finally section finalizes.

The try-finally block after the so initialization is there only to protect the SomeObject instance, to make sure it gets cleaned up no matter what else happens. If there are other things that need to run, but they aren't related to whether the SomeObject instance was property allocated, then they should go in another try-finally block, probably one that wraps the one I've shown.

Requiring variables to be assigned manually before use does not lead to real problems. It only leads to minor hassles, but your code will be better for it. You'll have variables with more limited scope, and try-finally blocks that don't try to protect too much.

If local variables had default values, then so in the first example would have been null. That wouldn't really have solved anything. Instead of getting a compile-time error in the finally block, you'd have a NullPointerException lurking there that might hide whatever other exception could occur in the "Do some work here" section of the code. (Or do exceptions in finally sections automatically chain to the previous exception? I don't remember. Even so, you'd have an extra exception in the way of the real one.)

Spacecraft answered 6/1, 2009 at 8:0 Comment(8)
why not just have an if(so!=null)... in the finally block?Choragus
that will still cause the compiler warning/error - i dont think the compiler understands that if check (but i m just doing this off memory, not tested).Gymnastics
I'd put just SomeObject so = null before try and put null check to finally clause. This way there will be no compiler warnings.Aneroidograph
Why complicate things? Write the try-finally block this way, and you KNOW the variable has a valid value. No null-checking required.Spacecraft
Rob, your example "new SomeObject()" is simple and no exceptions should be generated there, but if the call can generate exceptions then it would be better to have it occur inside the try-block so that it can be handled.Adorne
You could always just use an additional try-catch block around the whole thing.Morpho
First, Sjbotha, exceptions can be generated there. We could run out of memory, or, more likely, the SomeObject constructor could throw its own exceptions. We have no idea what it does.Spacecraft
Second, it would NOT be better to have the constructor call inside that try block. That's a try-finally block, which does NOTHING to handle exceptions. If an exception occurred, we'd be calling CleanUp on an uninitialized or null variable, and we'd be back to the same state as in my first code blockSpacecraft
L
12

Notice that the final instance/member variables don't get initialized by default. Because those are final and can't be changed in the program afterwards. That's the reason that Java doesn't give any default value for them and force the programmer to initialize it.

On the other hand, non-final member variables can be changed later. Hence, the compiler doesn't let them remain uninitialised; precisely, because those can be changed later. Regarding local variables, the scope of local variables is much narrower; and compiler knows when it's getting used. Hence, forcing the programmer to initialize the variable, makes sense.

Leonie answered 6/1, 2009 at 7:31 Comment(0)
U
12

Moreover, in the example below, an exception may have been thrown inside the SomeObject construction, in which case the 'so' variable would be null and the call to CleanUp will throw a NullPointerException

SomeObject so;
try {
  // Do some work here ...
  so = new SomeObject();
  so.DoUsefulThings();
} finally {
  so.CleanUp(); // Compiler error here
}

What I tend to do is this:

SomeObject so = null;
try {
  // Do some work here ...
  so = new SomeObject();
  so.DoUsefulThings();
} finally {
  if (so != null) {
     so.CleanUp(); // safe
  }
}
Unlettered answered 6/1, 2009 at 9:58 Comment(2)
Yup, ugly. Yup, it's what I do, too.Emalia
@ElectricMonk Which form you think is better, the one you shown or that shown here in the getContents(..) method: javapractices.com/topic/TopicAction.do?Id=126Ransome
S
12

The actual answer to your question is because method variables are instantiated by simply adding a number to the stack pointer. To zero them would be an extra step. For class variables they are put into initialized memory on the heap.

Why not take the extra step? Take a step back--Nobody mentioned that the "warning" in this case is a Very Good Thing.

You should never initialize your variable to zero or null on the first pass (when you are first coding it). Either assign it to the actual value or don't assign it at all because if you don't then Java can tell you when you really screw up. Take Electric Monk's answer as a great example. In the first case, it's actually amazingly useful that it's telling you that if the try() fails because SomeObject's constructor threw an exception, then you would end up with an NPE in the finally. If the constructor can't throw an exception, it shouldn't be in the try.

This warning is an awesome multi-path bad programmer checker that has saved me from doing stupid stuff since it checks every path and makes sure that if you used the variable in some path then you had to initialize it in every path that lead up to it. I now never explicitly initialize variables until I determine that it is the correct thing to do.

On top of that, isn't it better to explicitly say "int size=0" rather than "int size" and make the next programmer go figure out that you intend it to be zero?

On the flip side I can't come up with a single valid reason to have the compiler initialize all uninitialized variables to 0.

Steib answered 20/5, 2011 at 23:11 Comment(1)
Yes, and there are others where it more or less has to be initialized to null because of the way the code flows--I shouldn't have said "never" updated the answer to reflect this.Steib
A
6

For me, the reason comes down to this this: The purpose of local variables is different than the purpose of instance variables. Local variables are there to be used as part of a calculation; instance variables are there to contain state. If you use a local variable without assigning it a value, that's almost certainly a logic error.

That said, I could totally get behind requiring that instance variables were always explicitly initialized; the error would occur on any constructor where the result allows an uninitialized instance variable (e.g., not initialized at declaration and not in the constructor). But that's not the decision Gosling, et. al., took in the early 90's, so here we are. (And I'm not saying they made the wrong call.)

I could not get behind defaulting local variables, though. Yes, we shouldn't rely on compilers to double-check our logic, and one doesn't, but it's still handy when the compiler catches one out. :-)

Amesace answered 25/3, 2010 at 8:24 Comment(1)
"That said, I could totally get behind requiring that instance variables were always explicitly initialized..." which, FWIW, is the direction they've taken in TypeScript.Amesace
D
4

I think the primary purpose was to maintain similarity with C/C++. However the compiler detects and warns you about using uninitialized variables which will reduce the problem to a minimal point. From a performance perspective, it's a little faster to let you declare uninitialized variables since the compiler will not have to write an assignment statement, even if you overwrite the value of the variable in the next statement.

Dragging answered 6/1, 2009 at 7:12 Comment(7)
Arguably, the compiler could determine whether you always assign to the variable before doing anything with it, and suppress an automatic default value assignment in such a case. If the compiler can't determine whether an assignment happens before access, it would generate the default assignment.Vaughan
Yes, but one might argue that it lets the programmer know if he or she has left the variable uninitialized by mistake.Dragging
The compiler could do that in either case too. :) Personally, I would prefer that the compiler treats an uninitialised variable as an error. It means I might have made a mistake somewhere.Vaughan
I'm not a Java guy, but I like the C# way of handling it. The difference is in that case, the compiler had to issue a warning, which might make you get a couple hundred of warnings for your correct program ;)Dragging
Does it warn for the member variables too?Leonie
Nope, member variables will be initialized both in Java and C# by the compiler, if you don't specify a thing.Dragging
Re "it's a little faster to let you declare uninitialized variables": Doesn't the runtime (JVM) do it anyway (not a rhetorical quesion)?Schmeltzer
A
4

It is more efficient not to initialize variables, and in the case of local variables it is safe to do so, because initialization can be tracked by the compiler.

In cases where you need a variable to be initialized you can always do it yourself, so it is not a problem.

Attenuation answered 6/1, 2009 at 9:48 Comment(0)
U
3

The idea behind local variables is they only exist inside the limited scope for which they are needed. As such, there should be little reason for uncertainty as to the value, or at least, where that value is coming from. I could imagine many errors arising from having a default value for local variables.

For example, consider the following simple code... (N.B. let us assume for demonstration purposes that local variables are assigned a default value, as specified, if not explicitly initialized)

System.out.println("Enter grade");
int grade = new Scanner(System.in).nextInt(); // I won't bother with exception handling here, to cut down on lines.
char letterGrade; // Let us assume the default value for a char is '\0'
if (grade >= 90)
    letterGrade = 'A';
else if (grade >= 80)
    letterGrade = 'B';
else if (grade >= 70)
    letterGrade = 'C';
else if (grade >= 60)
    letterGrade = 'D';
else
    letterGrade = 'F';
System.out.println("Your grade is " + letterGrade);

When all is said and done, assuming the compiler assigned a default value of '\0' to letterGrade, this code as written would work properly. However, what if we forgot the else statement?

A test run of our code might result in the following

Enter grade
43
Your grade is

This outcome, while to be expected, surely was not the coder's intent. Indeed, probably in a vast majority of cases (or at least, a significant number, thereof), the default value wouldn't be the desired value, so in the vast majority of cases the default value would result in error. It makes more sense to force the coder to assign an initial value to a local variable before using it, since the debugging grief caused by forgetting the = 1 in for(int i = 1; i < 10; i++) far outweighs the convenience in not having to include the = 0 in for(int i; i < 10; i++).

It is true that try-catch-finally blocks could get a little messy (but it isn't actually a catch-22 as the quote seems to suggest), when for example an object throws a checked exception in its constructor, yet for one reason or another, something must be done to this object at the end of the block in finally. A perfect example of this is when dealing with resources, which must be closed.

One way to handle this in the past might be like so...

Scanner s = null; // Declared and initialized to null outside the block. This gives us the needed scope, and an initial value.
try {
    s = new Scanner(new FileInputStream(new File("filename.txt")));
    int someInt = s.nextInt();
} catch (InputMismatchException e) {
    System.out.println("Some error message");
} catch (IOException e) {
    System.out.println("different error message");
} finally {
    if (s != null) // In case exception during initialization prevents assignment of new non-null value to s.
        s.close();
}

However, as of Java 7, this finally block is no longer necessary using try-with-resources, like so.

try (Scanner s = new Scanner(new FileInputStream(new File("filename.txt")))) {
    ...
    ...
} catch(IOException e) {
    System.out.println("different error message");
}

That said, (as the name suggests) this only works with resources.

And while the former example is a bit yucky, this perhaps speaks more to the way try-catch-finally or these classes are implemented than it speaks about local variables and how they are implemented.

It is true that fields are initialized to a default value, but this is a bit different. When you say, for example, int[] arr = new int[10];, as soon as you've initialized this array, the object exists in memory at a given location. Let's assume for a moment that there is no default values, but instead the initial value is whatever series of 1s and 0s happens to be in that memory location at the moment. This could lead to non-deterministic behavior in a number of cases.

Suppose we have...

int[] arr = new int[10];
if(arr[0] == 0)
    System.out.println("Same.");
else
    System.out.println("Not same.");

It would be perfectly possible that Same. might be displayed in one run and Not same. might be displayed in another. The problem could become even more grievous once you start talking reference variables.

String[] s = new String[5];

According to definition, each element of s should point to a String (or is null). However, if the initial value is whatever series of 0s and 1s happens to occur at this memory location, not only is there no guarantee you'll get the same results each time, but there's also no guarantee that the object s[0] points to (assuming it points to anything meaningful) even is a String (perhaps it's a Rabbit, :p)! This lack of concern for type would fly in the face of pretty much everything that makes Java Java. So while having default values for local variables could be seen as optional at best, having default values for instance variables is closer to a necessity.

Ulrikaumeko answered 12/9, 2018 at 2:49 Comment(0)
C
1

Flip this around and ask: why are fields initialised to default values? If the Java compiler required you to initialise fields yourself instead of using their default values, that would be more efficient because there would be no need to zero out memory before you used it. So it would be a sensible language design if all variables were treated like local variables in this regard.

The reason is not because it's more difficult to check this for fields than for local variables. The Java compiler already knows how to check whether a field is definitely initialised by a constructor, because it has to check this for final fields. So it would be little extra work for the compiler to apply the same logic to other fields to ensure they are definitely assigned in the constructor.

The reason is that, even for final fields where the compiler proves that the field is definitely assigned in the constructor, its value before assignment can still be visible from other code:

class A {
    final int x;
    A() {
        this.x = calculate();
    }
    int calculate() {
        System.out.println(this.x);
        return 1;
    }
}

In this code, the constructor definitely assigns to this.x, but even so, the field's default initial value of 0 is visible in the calculate method at the point where this.x is printed. If the field wasn't zeroed out before the constructor was invoked, then the calculate method would be able to observe the contents of uninitialised memory, which would be non-deterministic behaviour and have potential security concerns.

The alternative would be to forbid the method call calculate() at this point in the code where the field isn't yet definitely assigned. But that would be inconvenient; it is useful to be able to call methods from the constructor like this. The convenience of being able to do that is worth more than the tiny performance cost of zeroing out the memory for the fields before invoking the constructor.

Note that this reasoning does not apply to local variables, because a method's uninitialised local variables are not visible from other methods; because they are local.

Cotangent answered 8/1, 2022 at 13:2 Comment(0)
S
0

Eclipse even gives you warnings of uninitialized variables, so it becomes quite obvious anyway. Personally I think it's a good thing that this is the default behaviour, otherwise your application may use unexpected values, and instead of the compiler throwing an error it won't do anything (but perhaps give a warning) and then you'll be scratching your head as to why certain things don't quite behave the way they should.

Shofar answered 6/1, 2009 at 8:10 Comment(1)
Re "Eclipse even gives you warnings": Isn't it the compiler (not specific to Eclipse)? - "Accessing an uninitialized local variable will result in a compile-time error."Schmeltzer
B
0

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.

Builtin answered 28/1, 2009 at 16:50 Comment(2)
Flat out wrong...all Java non-primitives are stored in the heap regardless of when and how they are constructedYawning
Before Java 7, instance variables are stored on the heap and local variables are found on the stack. However, any object that a local variable references will be found in the heap. As of Java 7, the "Java Hotspot Server Compiler" might perform "escape analysis" and decide to allocate some objects on the stack instead of the heap.Enlightenment
N
0

Instance variable will have default values but the local variables could not have default values. Since local variables basically are in methods/behavior, its main aim is to do some operations or calculations. Therefore, it is not a good idea to set default values for local variables. Otherwise, it is very hard and time-consuming to check the reasons of unexpected answers.

Nesmith answered 30/8, 2017 at 5:45 Comment(0)
C
0

Memory stack for methods is created at execution time. The method stack order is decided at execution time.

There might be a function that may not be called at all. So to instantiate local variables at the time of object instantiation would be a complete wastage of memory. Also, Object variables remain in memory for a complete object lifecycle of a class whereas, local variables and their values become eligible for garbage collection the moment they are popped from the memory stack.

So, To give memory to the variables of methods that might not even be called or even if called, will not remain inside memory for the lifecycle of an object, would be a completely illogical and memory-waste-worthy

Conflagrant answered 13/5, 2021 at 17:7 Comment(1)
who was saying, initialization of local variables would be wished for at object-creation-time? If one had decided to give them a default value, one would certainly do so only at the time of method call.Repetitive
F
-2

The answer is instance variables can be initialized in the class constructor or any class method. But in case of local variables, once you defined whatever in the method, that remains forever in the class.

Funkhouser answered 4/3, 2013 at 10:53 Comment(0)
M
-2

I could think of the following two reasons

  1. As most of the answers said, by putting the constraint of initialising the local variable, it is ensured that the local variable gets assigned a value as the programmer wants and ensures the expected results are computed.
  2. Instance variables can be hidden by declaring local variables (same name) - to ensure the expected behaviour, local variables are forced to be initialised to a value (I would totally avoid this, though).
Mattheus answered 30/12, 2014 at 21:20 Comment(2)
Fields can not be overridden. At most, they can be hidden, and I fail to see how hiding would interfere with a check for initialization?Nonsectarian
Right hidden.If one decides to create local variable with same name as instance, because of this constraint, local variable will be initialised with puposely selected value (other than the value of instance variable)Mattheus

© 2022 - 2024 — McMap. All rights reserved.