Hi Alex, here is an example and an explanation from the book Learning Java, 3rd Edition that might answer your question:
void myMethod { int foo; if ( someCondition ) { foo = 42; ... } foo += 1; // Compile-time error, foo may not be initialized }In this example, foo is initialized only if someCondition is true . The compiler doesn't let you make this wager, so it flags the use of foo as an error.
Fred----- Original Message ----- From: "Alex Parks" <mehgcap@xxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx> Sent: Sunday, December 02, 2007 11:23 PM Subject: RE: another java question
Out of curiosity, why can you say int b; or char c; but you must give a boolean a value?Finally done with blackjack!!!! I will have no more questions with which to bug you all... until the next project.Have a great day, Alex----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx To: <programmingblind@xxxxxxxxxxxxx Date sent: Sun, 2 Dec 2007 23:16:07 -0500 Subject: RE: another java questionInitialize the boolean to something, for example: false.Take care, Sina-----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of AlexParksSent: Sunday, December 02, 2007 9:33 PM To: programmingblind@xxxxxxxxxxxxx Subject: another java questionHi all, Sorry, but I have just one more question about this blackjackprogram. Forsome extra credit I can include a function that adds 11 to a handwith anace when the total of the hand is below 11 and adds 1 with an ace when the total is more than 11. I havethisfunction inside a method called getHandTotal(). I use a booleancalled aceand set it to true if the adding loop encounters an ace so that,after theloop, I can use an if statement whose condition is that thehandTotal wasgreater than 11 and ace==true. Inside this I subtract 10 from the hand (sameassubtracting the 11 that was added in the loop and then adding theone Iwant). The problem is that the compiler says that ace might nothave beeninitialized, but just after starting the method I say booleanace; Is itbecause I am setting ace to true inside a loop or something? Ihave no ideawhy it won't work. All times I call ace it is lower case. Ihave triedjust saying if (total>11 && ace and if (total>11 && ace==true butbothresult in that same error. Thanks for any help.Have a great day, Alex __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind__________ View the list's information and change your settings at //www.freelists.org/list/programmingblind__________View the list's information and change your settings at //www.freelists.org/list/programmingblind
__________View the list's information and change your settings at //www.freelists.org/list/programmingblind