Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable might not have been initialized

 
John Johnson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain why this code doesn't compile:
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable x might not have been initialized

The try catch might fail, and a parent of Exception might be thrown, such as Throwable. As such, the variable of x may not get initialized.

Assume the try block fails, and assume the catch block is never entered - then the variable will never be initialized.

try this:




-Cameron McKenzie
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some other interesting examples of if-else and switch also

and
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why below does not compile as well?? I thought I have cover all integer possibilities here.

 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of the if checks which will be done at the runtime.To make it a compile time check make n a compile time constant by declaring it final.Now the following code will compile fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic