• 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

Variable scope ??

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic