• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

when does a variable is initialized ?

 
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for a local variable :
i know that we need to initialize a local variable before using it

for static variable:

What does this error says ? variable 'j' should be initialized automatically as it is a static one, then why we get this error.
Why dont we get the same error for local variables. Why for local variable its says that it must be initialized before using instead of saying that we cannot use it before it is not defined ? At what stage a variable gets defined ? is it after completing the statement ?
 
Ranch Hand
Posts: 96
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java operators

Read the above article and pay particular attention to the right to left evaluation. It should make things clearer.

Cheers,
Wim
 
ashvinyin bhawalkar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, cool so what this article says is that

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.


This gives the answer for the question, so in both the cases
The compiler tries to evaluate the RHS of the '=' where it need i and j , and throws an error because it cannot find it.

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic