jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes does this make sense Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "does this make sense" Watch "does this make sense" New topic
Author

does this make sense

Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
in my code for a server, in one of the classes i must store 5 details, one of them is the no. of messages on a topic. in order to increment this by one everytime a new message is posted i have the following code, it compiles fine but as i am only starting this project can anybody tell me if this looks to have any problems:

public int getNoMsgs(){
return (NoMsgs);
}

public void incNoMsgs(int inc)
{
inc = getNoMsgs();
inc++;
incNoMsgs(inc);
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your public void incNoMsgs(int inc) method is recursive (i.e. it calls itself). How is a call to this method ever going to end?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Not only is it recursive, it really doesn't make sense. The first operation immediately overwrites the amount to increment...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
thanks guys,
so what to i want to do to it??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Originally posted by Chris Lavery:
thanks guys,
so what to i want to do to it??


I don't think that giving you the answer is a good idea -- as you won't learn anything... but since it is Thanksgiving, and I am already running real late...



Happy Turkey Day...
Henry
 
 
subject: does this make sense