File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable not initialed

 
Vijay Bhuruk
Ranch Hand
Posts: 31
MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



when i compile,get error. Error: variable b might not have been initialised.


only way to clear error is initialise b.(tested for b=4)

but when i tried below code,where i have not initialised variable b,it works too.



why?

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try this it will still give error...

The reason the first code gives error is.....
"a" is non-final varible means it can change its value later....
And the compiler know that if the if condition fails "b" will not be initilaized.So error says "using before initilzing"

In the second case "a" in final and compiler can be sure that the value will not change and the "b" variable WILL be initialized
 
Vijay Bhuruk
Ranch Hand
Posts: 31
MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James.
 
harilal ithikkat
Ranch Hand
Posts: 221
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James really cool answer.....................
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just adding my 25 paise..

If interested in this idiosyncrasy of the Java compiler, you might want to read JLS Chapter 16 - Definite Assignment which addresses this area.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic