简体   繁体   中英

Java multiple Classes and multiple main methods, execute all main methods

Im new to Java and I just wrote some Code in which I used two classes with main methods. Id like to execute both main methods, one after another. Is there a possibility to execute both of them at once in a specified order?

imFirst.java

public class imFirst {
    public static void main(String[] args) {
        System.out.println("I want to be the first one executed!");
    }
}

imSecond.java

public class imSecond {
    public static void main(String[] args) {
        System.out.println("I want to be the second one executed!");
    }
}

these are in one package, executed via eclipse.

You can call imSecond's main from imFirst:

public class imFirst {
    public static void main(String[] args) {
        System.out.println("I want to be the first one executed!");
        imSecond.main(args);
    }
}

Or can be the opposite:

public class imSecond {
    public static void main(String[] args) {
        System.out.println("I want to be the second one executed!");
        imFirst.main(args);
    }
}

Do it depending of your needs. But don't do both things at the same time or you can get an endless loop of both methods calling each other.

As a side note: use proper java naming conventions. Class names should be CamelCase.

Quick-Fix

You can call a main -method like every other regular method as well:

public static void main(String[] args) {
    imFirst.main(null);
    imSecond.main(null);
}

Better approach

But you should first think of why you even need two main methods at all. A main method is the first thing in the whole Java chain and usually you only use one for every complete program. The purpose is to simply start the program, most times its just a call to a dedicated class like:

public static void main(String[] args) {
    ProgramXY programXY = new ProgramXY();
    programXY.init();
    programXY.start();
}

So I recommend you to simply move both print statements into own classes and methods and then simply call them from one main method :

The utility class:

public class ConsolePrinter {
    public static void println(String line) {
        System.out.println(line);
    }
}

The only main-method:

public static void main(String[] args) {
    ConsolePrinter.println("I want to be the first one executed!");
    ConsolePrinter.println("I want to be the second one executed!");
}

More general

Or for a more general purpose:

First class:

public class FirstClass {
    public void firstMethod() {
        // ...
    }
}

Second class:

public class SecondClass {
    public void secondMethod() {
        // ...
    }
}

The only main method:

public static void main(String[] args) {
    FirstClass first = new FirstClass();
    SecondClass second = new SecondClass();

    first.firstMethod();
    second.secondMethod();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM