• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do these instances become the same?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do a and b become the same and how do I fix it so they keep their own values?


Expected Output:
x: 100 y:100
x: 250 y: 100

Current Output:
x: 250 y: 100
x: 250 y: 100
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x and y are static variables. Read up on static variables here and post again if you still don't understand the behavior.
 
stork weezard
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I was very confused.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

Remember: there are good reasons for writing static, but if you write static without being able to give a good reason, assume it is a mistake. Actually your thread title hides a bit of misunderstanding. You ask why the instances become the same. But static members do not constitute part of the instance; they belong to the class and are shared,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!