• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Increment

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output: 1 1 0 1 1

I couldn�t understand how r2.b and b are evaluating to 1 and why r2.a is evaluating to 0.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 instances (objects) of Red. r1 refers to the first. r2 refers to the other.

There are two copies of a, one in each object. r1.a refers to the first copy, r2.a refers to the other copy.

Thete is one copy of the static member variable b. It can be refered to as b, Red.b, r1.b, or r2.b, but there is still only one copy.

All three are member variables so they are initialized to 0. r1.a and the one and only b are then incremented to 1. r2.a is left at 0.

In the print line, the same b is printed three times.
[ January 02, 2005: Message edited by: Mike Gershman ]
 
Poop goes in a willow feeder. Wipe with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic