• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How compiler find for initialized/ uninitialized variable

 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just read that- Compiler can detect in very simple conditional cases, that a local variable will be initialized, but will complain in complex cases.
e.g.


Can anyone please explain it how to find whether compiler gives error or not!!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the condition is compile time constant, then compiler assumes that block executes.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In your first example either line 6 or 8 will be executed, so there is no path through the code where i would not be initialised.
If you meant to write just

and wondered why that compiled and the second example didn't, it's because true is a compile time constant. The compiler knows it will always be true, whereas in your second example b is a variable and even though it is set to true on the previous line, the compiler doesn't take that into account and so complains.
If you made b a final variable, it would again become a compile time constant and the compiler would be happy again.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
and wondered why that compiled and the second example didn't, it's because true is a compile time constant.

even if it is not a compile time constant, compiler wont complaint because one of the block definitely execute.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Stuart A. Burkett wrote:
and wondered why that compiled and the second example didn't, it's because true is a compile time constant.

even if it is not a compile time constant, compiler wont complaint because one of the block definitely execute.

My comment referred to the modified code in my post. There's only one block there, there's no else.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Look at the code. There's only one block there, there's no else.

Oh, Sorry. I have misinterpreted second example as another example. apologies...
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stuart and Seetharaman!! That clears my doubt!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic