Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variable initialization error

 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why is the compiler complaining at lines 2 and 3 that x is not initilized when it gets tha value 42 at line 1 ? ?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because it refers to it's declaration 'int x'. Who ensures that the try block will ever run to completion and assign a value to x ?

So, initialize the int upon declaration.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try -catch signify that there could be an exception, means we are not sure whether the particular line of code in try-catch will execute or not. So for compiler it is not sure that whether the line which is initializing the variable x will run or not and hence throws an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic