• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

shadowing

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems odd to me that VERSION 1 below gives compile time errors but VERSION 2 does not.


[This message has been edited by Randall Twede (edited December 10, 2000).]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Randall,
The answer to your question is related to scope of a variable within a method also known as local scope or method scope.
Version 1 : Since x is defined in method 1 (the first line of the method), the compiler objects to the variable declaration subsequently in the while and the for loop.
Version 2 : In Java there is no forward referencing. The variable x defined in while and for loop have local scope within their curly braces. By the time the compiler reaches the line where x is defined again, it does not find any conflict as the variables defined in while and for loop have their own local scope, hence does not complain. ( Again as Java does not have forward referencing, the compiler would not be aware of this declaration while resolving the while and for loop declared earlier).
HTH.....
Sudhir
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i understand but it still seems odd. especially considering the local variable has no problem shadowing the member variable. of course i would never write code like this. i only have to know what will happen.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good example for scope.
Version1
four declarations of x
1.member field - could be shadowed by local and it works
2.local field - declaration of int x ( shadowing)
3.while loop another declaration of int with the same name-error
(in method scope)
4.for loop -another declaration of int with the same name-error
(in method scope)
Version2
four declarations of x
1.member field
2.inside of while loop, exists in scope of curly braces.
3.inside of for loop, exists in scope of curly braces.
4.local declaration( shadowing)
...
for(int x=4;x<5;x++) {}
System.out.println( x );//will print 1
int x = 2;
System.out.println( x );// will print 2
}
}
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic