Search code examples
javaconventions

Java conventions for calling non-static methods from the main() method


I have been programming in Java for quite some time now but it was always just Android apps which don't start with a static main method. I want to know the conventions for "standard" Java programs because most of the time, I'm calling non-static methods which obviously can't directly be done through the main() method.

Here is an example program that I wrote (just printing a Fibonacci number). Is this an acceptable solution?

public class MainClass {
  public static void main(String[] args) {
    new MainClass().mainProgram();
  }

  public void mainProgram() {
    System.out.println(fibonacci(10).toString());
    // go on with the program
  }

  // suppose this method needed to be non-static
  public Integer fibonacci(int count) {
    if (count <= 0) {
      return null;
    }
    if (count == 1 || count == 2) {
      return 1;
    }
    // noinspection
    // (suppresses logically impossible null-pointer exception warnings)
    return fibonacci(count - 2) + fibonacci(count - 1);
  }
}

Solution

  • That's exactly how.

    Create an object instance. Call the methods on the instance.

    For example, this is a typical code in a Springboot application.

    An instance of SpringApplication is created and then the instance method run is invoked.

    public class Application {
    
        public static void main(final String[] args) {
    
            final SpringApplication application = 
                new SpringApplication(Application.class);
            application.run(args);
        }
    }
    

    As for the name of the "main" method, in the example above run makes sense because you want to run the application.

    But it could be anything like run, execute, start, serve, scan it depends on what your program does.

    In your specific case I would call it calculateFibonacci, or if your class was named Fibonnacci then just calculate()


    Is this how you would do it or what should I change?

    I think your code looks good.

    The way I would've written would be as follows

    public class Fibonacci {
    
       public static void main(String[] args) {
           Fibonacci fibonacci = new Fibonacci();
           fibonacci.execute();
       }
    
       public void execute(){
           System.out.println(fibonacci(10));
       }
    
       public int fibonacci(int n){
    
           if (n <= 0){
               throw new IllegalArgumentException(
                  "n must be greater then 0. Received: " + n);
           }
    
           if (n == 1 || n == 2){
               return 1;
           }
    
           return fibonacci(n - 2) + fibonacci(n - 1);
        }
    }
    

    The reason for the exception instead of null it to indicate that's an actual invalid (or illegal) argument. The program can't calculate, whereas null would indicate the computation for a negative number results in null which is not the case.

    But the way you have it is completely fine.