• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialization

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ! can anyone help me in this

I am getting the error "K might not have initialized" But in the code i am initializing k at stmt1
plz help
dhana
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JVM doesn't give default values for local variables and these variables must be initialized before you attempt to use them. infact, u must get an error with if(j==1) as it won't be initialized till runtime.
hope this helps!!
~ Shalini
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhana,
In java all local variables need to be initialized before any attempt is made to use them. In the first two if statements, you are checking with true conditions, which compiler can easily check during the compilation process. So, your program will compile cleanly if you remove the variable K from code. Since, you included K, compiler cannot check whether it is already initialized above, even though you are conditionally initializing K. Hope this helps..
Regards,
Rama
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taurean,
The compilor can evaluate some statments during compile time. These statements include the ones that uses only literals or final variables.
Thus if(true) is evaluated, so the compilor knows that the next statment will SURE be executed during runtime and thus it won't signal an error that i might not be initialized.
Same goes for if(1==1) as 1 and 1 are literals thus evaluated during compile time.
The last if statement can't be evaluated at compile time for the fact that j is neither a literal nor a final variable.
Hint: Try changing the first if statement to if(false) and compile.
What will be the result?
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot shalini, ramakumar and alfred..
Alfred the explanation u had cleared my doubt
dhana
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic