2

I've noticed that the JAVA allows to use uninitialized instance variable but it blocks to use uninitialized local variables. I want to know why the language is saying so?

Note : This is not the first post of this kind. I've visited below questions too. But couldn't find the exact reason for WHY? This question may be a duplicate one of the following:

Community
  • 1
  • 1
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • 2
    It's in the accepted answer on the first question: "It's not practical to enforce definite assignment on fields." That explains why there's a difference. It's not clear whether you're asking why local variables *must* be initialized, or why fields *don't* have to be initialized. – Jon Skeet Apr 02 '14 at 12:22
  • 1
    The best answer to your question would be found in the second thread you linked in your question in my opinion. I don't see the point in asking again, try to read that more carefully. Edit: also, like Jon Skeet says just as well. – Mena Apr 02 '14 at 12:22
  • @Jon Skeet : Yes Jon! I'm unclear why local variable must be initialized and why instance variables must not be initialized! – ironwood Apr 02 '14 at 12:35
  • @Namalak: No, my question was *which* of those you weren't sure on. Basically the existing questions provide answers to both - it would just change which I'd cast a close vote on... – Jon Skeet Apr 02 '14 at 12:37
  • Sorry Jon! I didn't get it once!. Comparatively it is much unclear that why local variable must be initialized by force. – ironwood Apr 02 '14 at 12:43
  • I am not sure of your premise - local (method/code block) variables "must be initialised"? How about perfectly valid code `int localVar; some code; some code; localVar = initvalue;`? Or are you asking why Jva does not allow you to **use** local variable **before** they are initialised? This is due to the fact that local variables are allocated in the stack and are **dirty** memory, it is inefficient to ask the system to clear this memory every time. – Germann Arlington Apr 02 '14 at 12:59
  • @Germann : Yes I'm asking about why Java does not allow you to use local variable before they are initialised? – ironwood Apr 03 '14 at 01:56
  • 1
    In this case the answer is in the last sentence of my previous comment. To clarify: instance variables only need to be initialised once per object life cycle, **new** local variables are allocated on **every** invocation of the method/code block. – Germann Arlington Apr 03 '14 at 07:53

2 Answers2

1

I've noticed that the JAVA allows to use uninitialized instance variable.

No, the compiler initialize instance variables, if you don't initialize.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

As I know,

  • Instance variable: will be initial at the run time when class initial and default of instance variable is null => instance variable will error at run time.
  • Local variable: Unlike class and instance variables, a local variable is fussy about where you position the declaration for it: You must place the declaration before the first statement that actually uses the variable. => local variable error with syntax error. ref: Local variable in java
Bui Anh Tuan
  • 910
  • 6
  • 12