aspose file tools*
The moose likes Beginning Java and the fly likes simple question.......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple question.........." Watch "simple question.........." New topic
Author

simple question..........

tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
i have the below class, Score:

class Score
{
int gamescore;

public void putscore(int s)
{
gamescore = s;
}

public int getscore()
{
return gamescore;
}
}

as u can see its a very simple class, and this is used to keep track of scores in a game. and everytime i call it from another class(which i have to), for example, s.putscore(3), i expect the variable gamescore to be changed to the value that i last inputed. however once it goes out of putscore the gamescore seems to get resetted to 0. i dont know why!! can anyone help?
Simon Baker
Ranch Hand

Joined: Sep 09, 2004
Posts: 57
Are you sure that you are calling getscore() on the same instance of Score that you called putscore() on?

Additionally, you ought to declare gamescore private, and the accessor methods public or protected (whichever is the most restrictive you can get away with).
Also, being pedantic, convention would suggest naming the methods getScore() and putScore(), and the instance variable gameScore. Note the capitalisation.
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hm, from my point of view thats impossible, at least from looking at the code you just presented. One thought though, could it be that the instance you are using to call getScore() on is a newly created one or that its another instance object than the one you called putScore() on?

Because when a new Score class is created the default value of gamescore will be 0.
[ October 06, 2004: Message edited by: Dominic Steng�rd ]

Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You class works fine, are you sure that you are not creating a new instance of Score each time you call it ? Check out the code below
If you want to create one and only one instance of score, you will need to apply the singleton pattern. As shown below
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by tan kian:

[ snip... ]

as u can see its a very simple class, and this is used to keep track of scores in a game. and everytime i call it from another class(which i have to), for example, s.putscore(3), i expect the variable gamescore to be changed to the value that i last inputed. however once it goes out of putscore the gamescore seems to get resetted to 0. i dont know why!! can anyone help?


Can you please post the exact code that produces the behavior described here? As other's have mentioned, your class works fine if you use it correctly. I suspect the problem is with your other code, not this class.

Layne


Java API Documentation
The Java Tutorial
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi,

another possibility, one that is only possible when you 'cleaned' the class before posting it here. Is that you have another variable declared with the same name inside your method putscore(int). By assigning the value to gamescore you actually asigns the value to a local variable (with a scope limited to the method putscore(int)), instead of the global (class member) variable. This results in keeping the global variable gamescore at 0.

Hope this helps?
Rikko
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
i cant really post my code, cause it involves about 3 files, and its gonna take up lots of space.......anyway i'll try the suggestions u guys gave and i'll get back here again.
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
ok, i found out the prob. as i said, my whole code involves 3 files, and i had created the Score() in one file, and created new instances of it in the current AND another file. so i used Nigel's method of Instance and it works fine. =) thanks all!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by tan kian:
i cant really post my code, cause it involves about 3 files, and its gonna take up lots of space.......anyway i'll try the suggestions u guys gave and i'll get back here again.


I'm sorry I should have been more specific. I was only requesting the portion of the code that does what you described in words in your original post. I doubt that we needed to see the whole program, but we did need to see more of the code than what you posted.

Anway, it looks like you've figured it out anyway. Good job and Keep Coding!

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple question..........