1

How do I call a function of the same class in java without using an object?

I tried this but got an error:

'non-static method facti(int) cannot be referenced from a static context' System.out.print(facti(number));

public class Facto {
    int i, fact =1;
     int facti(int num){
        if(num == 0){
            System.out.print("For Zero ");
            return 1;
        }
        else
        for (i = 1; i <= num; ++i)
        {
            fact = fact * i;
        }
        return fact;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number for factorial : ");
        int number = sc.nextInt();
        Facto f1 = new Facto();
        System.out.println(f1.facti(number));
    }
}
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Assuming that all that is wrapped in a class declaration similar to `public class Facto { ... }` then your code works fine for me. Java IS a Object Oriented Language, this means just about everything is a object, so you need to start with one – MadProgrammer Aug 18 '19 at 22:07
  • 2
    The answer is in your error. Make the method `static`. – Elliott Frisch Aug 18 '19 at 22:07
  • @ElliottFrisch I'm not sure it needs to be, since they use `Facto f1 = new Facto();` and `f1.facti(number)` and if you take the code and wrap it in a proper class declaration it works, something else is missing – MadProgrammer Aug 18 '19 at 22:08
  • @MadProgrammer I was addressing the question in the title, if you're going to call a function of the same class without using an instance of that class then the function must be `static`. As for OOP, you are of course correct. – Elliott Frisch Aug 18 '19 at 22:11
  • @ElliottFrisch Yeah, it's some what confusing :P – MadProgrammer Aug 18 '19 at 22:15
  • @MadProgrammer, my guess is that the code posted is not the code that generated the error that was posted. There is no `fi.` in the error. – Jim Rhodes Aug 18 '19 at 22:21
  • 2
    It's almost never a good idea to solve this error by making something `static`, but this is the exception; your `fact` is a *pure function* (depends only on its inputs and has no side effects), so it does not need to be associated with an instance of the class. – chrylis -cautiouslyoptimistic- Aug 18 '19 at 22:21

2 Answers2

1

Simple answer: you can't. Your main method is 'static' it only can call other static methods (in the same class) or methods of an object. So you either can make "facti" static, too. Or you create an object: Facto f = new Facto(); f.facti(13); and call facti on that object.

Angel O'Sphere
  • 2,642
  • 20
  • 18
-1

Methods in Java are bound to objects unless specified otherwise. For example, consider the function foo(String str) in the class Bar:

public class Bar {
    public void foo(String str) {
        doSomething(this);
        System.out.println(str);
    }
}

This method is "attached" to a particular instance of the class Bar, meaning that it is effectively passed in as another parameter (e.g. like foo(String str, Bar this)). In Java, this is a reserved keyword, and refers tho this "hidden parameter".

Since this method has this hidden parameter, you must call it on an instance of Bar, for example:

Bar bar = new Bar();
bar.foo("hello");

If you just call: Bar.foo(), the "hidden parameter" does not know what it should be, and so this fails at compile time.

If, however, you do not need to refer to a particular instance during your method, you can mark it as static. This means you can call it on a class, rather than a type, like Bar.foo();

The error you are receiveing (non static method cannot be refernenced from a static context) is saying:

  • you are trying to call a non-static method facti(int i) (or, with a "hidden parameter", facti(int i, Facto facto)).
  • The context (i.e. the method you called it from: public static void main(String[] args)) is a static method, and so does not have this hidden parameter.

To fix it, either:

  • Make your method static, to avoid the need for a particular instance of Facto
  • Create an instance using new Facto(), and call it on that: e.g. new Facto().facti(123);
cameron1024
  • 9,083
  • 2
  • 16
  • 36