This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi Every one, This is my first post in this site.. I am preparing for SCJP..planning to write within 10 days. I saw this program in javaprepare.com site in language fundamentals questions.. I ran this program and it is not compiling..giving error as y may not be inetialised.. my thinking is..as in 2nd if stmt y is getting 2 It has to compile and run and display 2 for y.. I want help..please clarify my doubt.. public class Compare { public static void main(String args[]) { int x = 10, y; if(x < 10) <br /> y = 1;<br /> if(x>= 10) y = 2; System.out.println("y is " + y); } } Thanks in advance.. Regards radhika
Fred Abbot
Ranch Hand
Joined: Jun 01, 2000
Posts: 300
posted
0
since you initalizing Y inside an if()statement it may not be initalized as if the if()is false then Y will never be initalized this post should be in anothe rthread not here
radhika madini
Greenhorn
Joined: Jan 10, 2001
Posts: 18
posted
0
Hi Fred Abbot,
Originally posted by Fred Abbot: since you initalizing Y inside an if()statement it may not be initalized as if the if()is false then Y will never be initalized this post should be in anothe rthread not here
you mean if we assign values inside if loop those values are not visible outside that loop.. am i correct in this..
radhika madini
Greenhorn
Joined: Jan 10, 2001
Posts: 18
posted
0
Hi Fred, I ran the program by assigning value for y outside if loop and it is working.. Thanks alot for your help.. Regards Radhika
ashwini srinivasan
Greenhorn
Joined: Nov 07, 2000
Posts: 26
posted
0
radhika, y is a local variable. Local variables must be initialized before u use it....bcaz local variables will not get automatically initialized to default. So u need to initialize it and then can change its value in if() statement.Hope this helps u.
Fred Abbot
Ranch Hand
Joined: Jun 01, 2000
Posts: 300
posted
0
hi ashwini although what you are saying is correct that is not the issue at hand here as she is initalizing it before using it the problem however is that it is being initalized inside an if() statement so there will betimes that it is not initalized hence the error is y MAY NOT have been initalized in your case the error would be y WAS NOT initalized
Fred Abbot
Ranch Hand
Joined: Jun 01, 2000
Posts: 300
posted
0
Originally posted by radhika madini: Hi Fred Abbot, you mean if we assign values inside if loop those values are not visible outside that loop.. am i correct in this..
what you are saying is true but againg is not your origanal question if you would initalize y to 0 before the loop then inside a loop change it to 5 it will be 5 outside the loop as well however it cannot be initalized for the first time inside the loop since it may never get inside the loop
radhika madini
Greenhorn
Joined: Jan 10, 2001
Posts: 18
posted
0
Hi Fred, I tested what u said by assigning y=0 inetially.then it is printing the value assigned inside if{} ie 2.I got your concept.. one more thing is.. is it same for do and while loops also?
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
sunil choudhary
Ranch Hand
Joined: Nov 10, 2000
Posts: 138
posted
0
As you will notice in the fllowing program the initialisations in a If method cannot be taken as initialisations as they are subject to conditions public class Compare { public static void main(String args[]) { int x = 12, y; for(;x<10;y=1) <br /> {<br /> System.out.println("in ist for");}<br /> for(;x>= 10;y=2) {public class Compare { public static void main(String args[]) { int x = 1, y; if(x>= 10) { System.out.println("in iist for");} System.out.println("y is " + y); } } //Thanks in advanc System.out.println("y is " + y); } } //the program refuses to take initilaisations for value of 1 as well as of 10 or 11.
----------------------------------<br />"Learning is weightless, a treasure you can always carry easily."<br />-Chinese Proverb