• 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

increment ???

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

On the above code, why it display 2 for both t1.i and t2.i ?
I taught it only have increment by one!
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You create two instances of T, since i is static member, it belongs to the class, not to any specific instance. Hence, every instance increments the same varibale i, that's to say, that one that belongs to T, every time the constructor of T is invoked.

The case of j is different. It is an instance member, and every instance of T will get a brand new initialized value for j. To this one you assign the number 1 in every case when the constructor of T is invoked.
[ May 27, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is reccomended that you use the Class name to refer to the static member instead of the object's instance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic