Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes variables may not be initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variables may not be initialized" Watch "variables may not be initialized" New topic
Author

variables may not be initialized

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am setting up a simple dice game and need to enter the players names. After creating the loop to go through and check and assign values to the different players names variables, the compiler tells me that the variables in my if else if statement may not have been initialized ( the compiler points specifically to the variables in the arguments section inside the () of each if and else if check). The problem is I am pretty sure I declared them properly. Maybe I missed something?



I appreciate any help in the matter
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

Say the user inputs "fred" for the first name. they are asked if they want to enter another name, and type 'q'.

What will playerTwo be initialized to?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
well, I guess nothing. Never sure how many players there would be to play the game, so I declared up to six. Does java need to have the variables holding a value to be happy?
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Ben Hultin wrote:well, I guess nothing. Never sure how many players there would be to play the game, so I declared up to six. Does java need to have the variables holding a value to be happy?


:_D

Is a good way of saying it. You can initialize them to String playerX = "";
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

Java REQUIRES all instance variables to be initialized. So yes, your best bet is to do "String x = "";"
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Well,

I thought all instance variables got their respective defaults if not initialized. local variables on the other hands were the ones I thought Required initialization before use.

Let me know if what i just said is total craziness...

Jose.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39810
    
  28
Yes, fields have default values of null/0/false. As you say, local variables do not have default values and must be initialised.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

Jose Campana wrote:Well,

I thought all instance variables got their respective defaults if not initialized. local variables on the other hands were the ones I thought Required initialization before use.

Let me know if what i just said is total craziness...

Jose.

And these Strings appear to be local variables.
 
Consider Paul's rocket mass heater.
 
subject: variables may not be initialized