# Can a method in a class call itself?

Dinesh Kumar Tadepalli

Greenhorn

Posts: 15

posted 6 years ago

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

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

posted 6 years ago

compute method compiled?

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

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

posted 6 years ago

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

posted 6 years ago

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.

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