Why are local variables not initialized in Java?

后端 未结 15 698
不知归路
不知归路 2020-11-22 09:28

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,

相关标签:
15条回答
  • 2020-11-22 09:39

    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.

    0 讨论(0)
  • 2020-11-22 09:39

    (It may seem strange to post a new answer so long after the question, but a duplicate came up.)

    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. :-)

    0 讨论(0)
  • 2020-11-22 09:40

    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
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:42

    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. Compiler knows when its getting used. Hence, forcing programmer to initialize the variable makes sense.

    0 讨论(0)
  • 2020-11-22 09:42

    The answer is instance variables can be initialized in 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.

    0 讨论(0)
  • 2020-11-22 09:44

    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 initialize it.

    0 讨论(0)
提交回复
热议问题