It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How compiler find for initialized/ uninitialized variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How compiler find for initialized/ uninitialized variable" Watch "How compiler find for initialized/ uninitialized variable" New topic
Author

How compiler find for initialized/ uninitialized variable

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
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

Joined: Jan 28, 2008
Posts: 5575

if the condition is compile time constant, then compiler assumes that block executes.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 30, 2012
Posts: 679
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 30, 2012
Posts: 679
No problem
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Thanks Stuart and Seetharaman!! That clears my doubt!!
 
Don't get me started about those stupid light bulbs.
 
subject: How compiler find for initialized/ uninitialized variable
 
Similar Threads
Lang fundamental
Switch Case Problem
compiler-time condition
How to differentiate between compile time errors and runtime errors ?