Main method empty?

4k Views Asked by At

I have a class that implements Runnable, but Eclipse needs a public static void main method in it. Is it ok if the main is completly empty?

public class Launcher implements Runnable{
private String message;

public Launcher(String message) {

    this.message= message;
}

public static void main(String[] args) {
}

@Override
public void run() {

//My implementations
}
2

There are 2 best solutions below

1
On BEST ANSWER

If you intend Launcher to be the main class of the application, the one you use to start it, the main method is needed and must do whatever should be done to start work.

If not, delete the main method. Eclipse does not require a main method unless you start the application by telling it to run the class. It will optionally generate one when creating a class, but that can be edited out if not needed.

0
On

No, the main method is the only method the compiler is searching for when looking where to start. Hence if you're main method is empty, nothing gets executed. At the very least add:

new Launcher("some string").run();

in the main method.