• 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 with Same Name

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does this code Compile ? Java is supposed to complain in case of identical variable names.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the first i is class level while the 2th is the local level ( int the main method),
the 2th "shadow" the 1th.
got it?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the scope of the variable is different in both case.
The first one will be the instance variable which you can access only through the instance of the class(unless its static variable).The second one will be within the scope of the Main method.
The compiler will complain only if you add duplicate instance variables or duplicate variable within the same scope (for ex. within a method).
I hope the above explanation is right.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, adding to what Ramki said, local variable, already declared in an enclosing block and therefore visible in a nested block, cannot be redeclared in a nested block. A local variable in a block can be redeclared in a block if the blocks are disjoint.
But this is not the case when the local & member variable have same name. Local variable shadows the member variable and takes priority.
Regards
Shweta
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shweta,
Ok so that means the following code should not work because
int i declared in LOOP method is visible to SECONDLOOP and hence declaring int i again in SECONDLOOP should throw Compilation error.
I compiled the code using JDK 1.4 and it executed correctly with the following output --
Inside Loop method --> i : 1
Inside SecondLoop method --> i : 2


Explanation please ?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arijit,
Every method has its own local memory available. It does not matter when same named local variable is used in different methods. But the following code will give u erros

I hope this helps
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arjit,
In ur code, u have not created nested loop, rather u r calling an entirely different method & as Anup says every method has itz own local memory. Method secondloop() is not using the memory of loop(), so there will be no conflict.
Arjit the following article from Cindy Glass may also help clearing some of your doubts.
Not all Variables are created Equal - Cindy's Segment - a Whimsical View of the World
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic