This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The Static varible will be created and initilized before the static block. As Balazs point out in his post, The x created in the static block is a new x which only has scope in the static block. Instead of using int x = 5 in the static block, try using this.x = 5 or Static.x = 5. The static varible has to exist before you can work with it. Therefore, the static varible is created and initilized before the static block.
how can we use the this operator in the static block ???
thanks & regards
Thanks & Regards<br /> <br />-Srikanth
Joined: Aug 16, 2005
Hi Swapna Thanks for your reply.. itried it again .yeah!!! i got the output now.
I thnk previouly i've left some of the println stmts in the program as they are.tha's why i was getting the value 5. Now i tried it again..I got it . thank you
Joined: Jul 26, 2005
In the explanation with the code I gave before said that the static block variable will not have any effect on the output because it declares a new variable. I got it. But what if I want to access that static block variable in the main method, can I do it? If yes how do I do it?
I am so confused about this static blocks and variables .