| Author |
where is the bug?
|
Antony Amicone
Ranch Hand
Joined: Mar 11, 2006
Posts: 125
|
|
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
Joined: Jul 08, 2003
Posts: 24054
|
|
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!
|
[Jess in Action][AskingGoodQuestions]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
All your "TimeOfDay" variables are static, there is one set for all your instances. Hence, changing one, changes all of them. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Antony Amicone
Ranch Hand
Joined: Mar 11, 2006
Posts: 125
|
|
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
Joined: Mar 11, 2006
Posts: 125
|
|
so... the inverse problem with this one [ March 27, 2006: Message edited by: Antony Amicone ]
|
 |
Antony Amicone
Ranch Hand
Joined: Mar 11, 2006
Posts: 125
|
|
ok i'm and idiot... i've not written the creation of the object calcolatrice... now i've made it all resolved tnx all
|
 |
 |
|
|
subject: where is the bug?
|
|
|