• 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

shadowing local variable

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JMM125 {
static int i;
public static void main(String args[]) {
for (i=1; i<3; i++) {System.out.print(i);} // 1
for (int i=1; i<3; i++) {System.out.print(i);} // 2
int i; // 3
for (i=0; i<2; i++) {System.out.print(i);} // 4
System.out.print(JMM125.i);
}}

output: 1212013
I am not getting how the 3 comes in while invoking (JMM125.i)?
what are all the things happening here with int i?
Please explain me this...
i am running out of this concept...

Preparing SCJP5
Thanks in advance
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the last 3 is from the statement

System.out.print(JMM125.i);

It was set to 3 by the first for loop...

All the three loops are using different i. The first loop uses the static i in class JMM125, the second one uses it's own local i, whose scope is inside the loop. The third loop uses i, which is local to main method...
[ October 12, 2008: Message edited by: Ankit Garg ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JMM125.i -> it is not all considering any of the local variablse. it deals with the class variable, i. since the value of i is used in the first for loop and incremented to 3, the value displayed would be 3.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeh coz the classname.variable name is used you are refering to the static variable declared globally.


[ October 13, 2008: Message edited by: Rajasekhar Devi Reddy ]
[ October 13, 2008: Message edited by: Rajasekhar Devi Reddy ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the concept works here...but the i value is supposed to be 2.How come it's getting 3?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because in first loop i's final value was 3, since the condition was i<3, the control came out of the loop, though inside the first loop the i's value is 2, after the loop got over, the value was 3
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi



This loop will terminate only when the condition, i<3 fails. ie, only when i==3. so the final value of i, after this loop will be 3.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic