aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with if clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with if clause" Watch "problem with if clause" New topic
Author

problem with if clause

Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
hi all
when i tried to compile the following code i got an error message saying that the variable y might not have been initialised.I could not understand this since the condition in if will always be true and in that the variable y will be initialised.Can somebody throw some light on this.
thanks in advance.



Thanks and Regards,<br />Manish Agarwal<br />SCJP 1.4 (100%)<br />SCWCD 1.4 (98%)<br />SCBCD 1.3 (Preparing)
Kris Adams
Greenhorn

Joined: Jul 02, 2004
Posts: 17
The compiler wouldn't evaluate the condition in the if clause. So all it sees is that the initialization is done conditionally and thats the reason for the error.

~SJCP
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Manish,
the compiler can analyse your code to a certain extent but it cannot predict that the value of x is not going to change before that if statement. It does not "run" your code. If, however, you tell the compiler that x is final then the compiler can do a better analysis and the code compiles cleanly.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108

The compiler wouldn't evaluate the condition in the if clause. So all it sees is that the initialization is done conditionally and thats the reason for the error.

I dont quite agree with Krishna ...
If the code is modified to include an else along with the if then it works just fine !



which means that the compiler checks to see if the variable y is initialized or not...In your case...If x were not 5 thn y would never be initialized. So if u have an else clause, it makes sure tht y is initialized all the time.
Also if u have something like this..

if(true)
y = 0;

then the compiler does not complain..
[ August 10, 2004: Message edited by: Murtuza Akhtari ]

---<br />SCJP 1.4
Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
thanks Krishna and Barry.
I hav one more query regarding if and while.When i use while(false) i got an error saying Unreachable statement.But if i use if(false) code compiles fine.Why no error in case of if?Please help me.
thanx.

Aarti Dahiya
Greenhorn

Joined: Aug 07, 2004
Posts: 15
Making x final still gives a compilation error.
The only thing that might compile this code is using if(true). The compiler knows that the condition is TRUE always, and does not depend on anything else.


SCJP 1.4 (95%)
Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
thanx Murtuza for extra info.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Aarti Dahiya:
Making x final still gives a compilation error.


Interestingly I do not get any compilation errors using the 1.5.0 Beta2 compiler. I'll try it out at work tomorrow using the 1.4.2 compiler.
Aarti Dahiya
Greenhorn

Joined: Aug 07, 2004
Posts: 15
Yes, Barry you are right! Making x final compiles just fine.
I copied the code wrong when I compiled it.
Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
Barry, but why the compiler does not complain when we use else along with if ?
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi ,



but for me it is compiling and giving out put also even without declaring x as final . pls help me in getting it




bye
siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with if clause