Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

variables may not be initialized

 
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
 
lowercase baba
Posts: 12871
62
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?
 
Ranch Hand
Posts: 308
  • 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
Posts: 12871
62
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 = "";"
 
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.
 
Marshal
Posts: 69771
277
  • 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
Posts: 12871
62
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.
 
pie. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic