• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why I have this error?  RSS feed

 
kk lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why my compiler said that x can not be found in the following code


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kk lin wrote:Why my compiler said that x can not be found in the following code




You defined x inside the if {} block. A variable only exists in the narrowest scope in which it's declared, so once you hit the end of if {}, x no longer exists. You could do this, for example:

 
kk lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, now I understand
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!