This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can a method in a class call itself? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a method in a class call itself?" Watch "Can a method in a class call itself?" New topic
Author

Can a method in a class call itself?

Dinesh Kumar Tadepalli
Greenhorn

Joined: Jun 17, 2009
Posts: 15
I was trying to code a factorial program. I have a doubt regarding if a method in a class can call itself? For example:



And in another test class.. I make an object of this class and ask the object's method for computing the factorial. Like:



Will this above strategy work ? I am beginner and when i ran this style of code, it was giving lots of exceptions. I need some help regarding this.

Thanks, Dinesh
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Dinesh Kumar Tadepalli wrote:



compute method compiled?

Dinesh Kumar Tadepalli wrote:
And in another test class.. I make an object of this class and ask the object's method for computing the factorial. Like:



Will this above strategy work ?


you can do this definitly. this relationship is known as Composition . try to debug yourself
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

This concept of a method calling itself is called Recursive function. You can do this but you need to be a little careful. Your compute method is doing wrong this. First of all, there is no return statement. If there was, then it would infinitely keep calling itself resulting in a StackOverflowError. You need to put a condition in your method body which will stop it from endlessly calling itself



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

I read a joke once about a programmer who died of starvation in the shower. They found a bottle of shampoo in his hand with the directions "Lather. Rinse. Repeat".

computers would actually work that way. They lather, they rinse, they repeat - which tells them to lather, then rinse, then repeat - which tells them to lather, then rinse, then repeat - which tells them to lather...

The idea behind recursion is that each call doesn't try and SOLVE the problem, but it tries to break the problem into a smaller problem which CAN be solved. One (or potentially both) of those problem may need to be broken down again into even smaller components.

Eventually, you get to some trivial case that can be easily solved, so you then wind your way back up. So in Englisgh...

5! might be too tough to handle. But, you know that

5! = 5 * 4!. So now, if you know 4! , the rest is EASY. so, call a method to compute 4!.

Well, 4! is too hard on it's own, but you know that 4! = 4 * 3!. so, let's compute 3!.

3! is too hard on it's own, but you know it's equal to 3 * 2!.

2! is equal to 2 * 1!, so let's compute 1!.

1! is trivial. it's 1. so, now we go back up...

2! = 2 * 1!, and we know 1!, so we can compute the answer and get 2. pass that back up.

3! = 3 * 2! (which we now know), so we get 3 * 2 or 6. pass that back up...

4! = 4 * 3! = 4 * 6 = 24. pass that back up....

5! = 5 * 4! = 5 * 24 = 120. Now we're done.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Didn't he die because he washed the top of his head away?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a method in a class call itself?