• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables may not be initialized

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java REQUIRES all instance variables to be initialized. So yes, your best bet is to do "String x = "";"
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic