Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where is the bug?

 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've 2 class, in class ProgCalc i've got a method that set a choose from a menu. I create "t" an object TimeOfDay, and i get its value, than i create another object TimeOfDay "t1". t1 overwrite t... why?



-----------------------------------

[ March 27, 2006: Message edited by: Antony Amicone ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a member variable is "static", it means it is shared by all instances of the class. Therefore although t and t1 are pointing to distinct instances of your class, they share all their member data. If t makes a change, t1 sees that change, and vice-versa.

Remove every single instance of "static" in the TimeOfDay class, and things will be fixed -- or at least close to fixed. Then go back and remove every single use of "static" in ProgTime, as well, except for the one in "public static void main()". Finally, change the call to printHome() to something like

new ProgTime().printHome();

and you'll be in great shape!
 
Henry Wong
author
Marshal
Pie
Posts: 21203
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your "TimeOfDay" variables are static, there is one set for all your instances. Hence, changing one, changes all of them.

Henry
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it runs!!! tnx a lot to everyone...
I made it without static, but eclipse gave me errors on method calling in main, because i only wrote printHome();

tnx a lot...

by the way, when it is better to set static a method?
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so... the inverse problem with this one



[ March 27, 2006: Message edited by: Antony Amicone ]
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i'm and idiot... i've not written the creation of the object calcolatrice...
now i've made it all resolved tnx all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic