Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increment ???

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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!
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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 ]
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic