• 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

Doubt in the output of this program.

 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand why the second System.out.print(i);// output is 0 is producing the output 0.

Since i is a static variable I think it should have displayed 1. Please clarify.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are calling that increment function, you are passing a value 0 to it. It gets the value in that parameter variable i (not the static variable i), increases that and returns the incremented value of the parameter variable, means the static variable i is still unaffected.
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please clarify, the second println statement produces the output 1, not zero. In the future, please use the code tags when you post
a piece of code. It makes the code more readable.

What happens is that the method increments the local variable i, not that static variable because it has been
shadowed.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pinki Roy wrote:Since i is a static variable I think it should have displayed 1


1) int variables are never initialized to 1 automatically.
2) while automatically initializing a variable, only two things matter : a) if it is a class variable(method local variables are never initialized to default value, except arrays); b) data-type of that variable. It doesn't matter if the variable is static or not.

Besides this, why do you think first print statement should print 1?
 
Pinki Roy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph , Thank you so much for the clarification.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Mokenela wrote:method increments the local variable i, not that static variable because it has been shadowed.


and I thought Pinki Roy is concerned about output of first print statement (I got confused because statement and comments are exactly same)
reply
    Bookmark Topic Watch Topic
  • New Topic