I have wrote a print method which tell the user congratulates on their, but at the same time i have another field called Downloads, I wouls like to update the field downloads once the thr print method have been invoked, thanks and im using bluej
Asked
Active
Viewed 103 times
-7
-
2Is there a question? I can't see any apart from your `wishlist`. – Rohit Jain Oct 31 '12 at 19:07
-
The implicit question is pretty clear, I think. "How do I update the `downloads` field once the print method has been invoked?" – asteri Oct 31 '12 at 19:10
-
where is downloads field? the question is hard to comprehend until explained clearly. – Arham Oct 31 '12 at 19:13
-
this is the field private int noOdDownloads; – tatu_kabo Oct 31 '12 at 19:15
-
yes, so whats wrong in your existing code? It's doing exactly that. Also you could optimize your code by looking at gefei's answer – Arham Oct 31 '12 at 19:26
1 Answers
2
instead of
int num;
this.noOdDownloads = num;
this.noOdDownloads = num + 1;
do
this.noOdDownloads++;
your original program has two problems: 1) num
is not instantiated, this is what the compiler will complain, and 2) even if you instantiated it, say with int num=0;
,
num
is actually a local variable in the method. every time the method is run, the local variable is instantiated again and reset to 0
. When you then assign its value to noOdDownloads
, the latter is reset to 0
too.
![](../../users/profiles/1180720.webp)
gefei
- 16,986
- 6
- 47
- 64