0

I am trying to calculate the factorial of some integer number (from 0 to 21) by using recursive method. I created a separete method outside the main class. But while printing the factorial in main method it shows an error. How to fix that?

package looping;

import java.io.PrintStream;

public class Looping {
    public long fact(long num)
    {
        if(num<=1)
        return 1;
        else
            return num*fact(num-1);
    }
    public static void main(String[] args) {

        for(int i=0;i<=21;i++){
         System.out.printf("%d!=d\n",i,fact(i));
        }

    }
}
Lubna
  • 51
  • 4

6 Answers6

2

You can not call fact() method without creating object of Looping class.

public static void main(String[] args) {
    Looping loop=new Looping()
    for(int i=0;i<=21;i++){
     System.out.printf("%d!=%d\n",i,loop.fact(i));
    }

}

or another way is creating fact() as static method of the class.

public static long fact(long num)
{
    if(num<=1)
        return 1;
    else
        return num*fact(num-1);
}
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
1

static method only call static methods so just declare your method static

public static long fact(long num){

}
shreyansh jogi
  • 2,082
  • 12
  • 20
1

There are two things you can do.

Either

(1) You can make fact a static method, by putting the word static after public in its declaration.

Or

(2) You can make an object of the Looping class and use it to run the calls to fact, like this.

public static void main(String[] args) {

    Looping looping = new Looping();
    for(int i=0;i<=21;i++){
     System.out.printf("%d!=%d\n", i, looping.fact(i));
    }
}

It doesn't make any difference which you choose, because an object of the Looping class doesn't have any fields, which means it doesn't carry around any data with it. It doesn't matter whether you make such an object or not; except for the fact that to use a non-static method, you need an object.

If the object had some fields, then a non-static method would be one that can refer to those fields. But since there are no fields, it makes no difference whether a method is static or not. Therefore, I'd probably choose option (1) here - just make the method static.

However, if, in the future, I want to have variations on the Looping class - that is, subclasses, each with a slightly different implementation of the fact method; then I really need to pick option (2). You can't really have different versions of a static method, within a subclass. When you write a class, you don't really know whether, in the future, you'll want some subclasses of it with slightly different behaviour. These may be subclasses that are going to be used in your program, or they may be things like mock subclasses that you use for testing.

Therefore, as a matter of course, it's always worth avoiding static methods. The only static method that any class ever needs to have is the main method. Any other static methods are likely to cause you problems, as your program grows and you have more and more classes. Static methods can make testing harder. They definitely make it more difficult to implement polymorphism.

I realise that this argument might be beyond where your current Java knowledge is. But it would be worthwhile understanding what's happening in option (2), and getting into the habit of using it, rather than making all of your methods static by default.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Maybe you can say - "(1) You can make fact a static method, by putting the word static after public in its declaration." (Not suggested due to efficiency reasons) " :P.. BTW +1 for bringing out both methods.. – TheLostMind Dec 27 '13 at 11:01
  • Why would I say "not suggested due to efficiency reasons"? I absolutely suggest option (1). – Dawood ibn Kareem Dec 27 '13 at 11:03
  • I think its always better to create an object to access a method rather than making the method static and accessing it via the class... Well, my apologies... Its just "my" thinking... – TheLostMind Dec 27 '13 at 11:05
  • 1
    @TheLostMind Well, you're right, but the reasons are a bit more subtle than you've enunciated. I've expanded my answer in light of your comments. – Dawood ibn Kareem Dec 27 '13 at 11:29
0

/inside the main/

Looping looping = new Looping();
for(int i=0;i<=21;i++){
    System.out.printf("%d!=d\n",i,looping.fact(i));
}
barthezzko
  • 17
  • 2
0

static means "does not need to be inside an objekt created with new"

Non-static means you need to be inside such an object. this must be available.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Why my output is like this? 0!=d 1!=d 2!=d 3!=d 4!=d 5!=d 6!=d 7!=d 8!=d 9!=d 10!=d 11!=d 12!=d 13!=d 14!=d 15!=d 16!=d 17!=d 18!=d 19!=d 20!=d 21!=d It doesn't calculate the factorial – Lubna Dec 27 '13 at 11:18
  • You are missing % with second "d"....System.out.printf("%d!=%d\n",i,loop.fact(i)); – Ronak Jain Dec 27 '13 at 11:19
0

it seems you forgot to create object of class

  Looping loop=new Looping();
    for(int i=0;i<=21;i++){
     System.out.printf("%d!=d\n",i,loop.fact(i));
    }
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52