Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable scope ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable scope ??" Watch "Variable scope ??" New topic
Author

Variable scope ??

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,




If the //2 and //3 are uncommented .with //1 commented ...then ...the program compiles fine ... and prints 66..


but if //1 and //2 are uncommented with //3 commented then there is a compilation error saying duplicate variable i..

Whats the reason..?

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Hi Kumar!
Outer things can get inside... but inner things can not come out.


Travelling from top to bottom... the "i" at line 1 is declared and registered. When execution comes inside the braces, the variable "i" is found to have been registered already.

But if you put the outer "i" to the bottom somewhere at line 3, the registration for inner "i" gets expired.
[ October 03, 2005: Message edited by: Akhil Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Your explanation is good..

but dont you think that whether i declare the variable at //1 or //3 ..it

still would be viewed within the same namespace...at the method level..

So the scope that should be considered should be the whole block rather than the sequence being the deciding factor..

What do you say...



Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Hmm.... I understand your argument, but the very general assumption is, "flow of execution is alway from top to bottom"(from package statement then import to downside), and the other way round i.e bottom to top is not possible or I must say is simply unimaginable. And I guess it is on this basis, considering about the "i" (at line 3) at line 2 or at line 1 itself looks absurd.
[ October 03, 2005: Message edited by: Akhil Trivedi ]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi...akhil..


but the very general assumption is, "flow of execution is alway from top to bottom"


yes during execution ....we can say that line by line when it is executing... it will first see the i in the inner block and then when it comes out of it...this i is out of scope and the next i //3 comes into scope...

i in //3 is available throught the class...thats the opinion formed by looking at the class...Similiarly i in //1 is also available...in the scope..

In java we can use a variable only after declaration...So if we consider this....

//1 is uncommented....and //3 is commented then ..

..it is as per your explanation..

//3 is uncommented and //1 is commented then ..

the inner block i is out of scope after the block and outside the block ...another i is newly declared that is accessible for the rest of the program..


This code ..wil help in the direction..






If you comment the println ...it would compile fine..

Thanks akhil..for the point..
 
Consider Paul's rocket mass heater.
 
subject: Variable scope ??
 
Similar Threads
Error catch Q
Variable initialization revisited.
why is the code complining
Declaring float & int.
Final variable Q