This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes where is the bug? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where is the bug?" Watch "where is the bug?" New topic
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: 24183
    
  34

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: 18765
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where is the bug?