This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes when does a variable is initialized ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when does a variable is initialized ?" Watch "when does a variable is initialized ?" New topic
Author

when does a variable is initialized ?

ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

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 ?
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Java operators

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

Cheers,
Wim
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when does a variable is initialized ?
 
Similar Threads
Question on static initializers..
Compile time constants
Strange result in my programm JTEST
Please Help !!!!
it's about "Argument Passing"