tags:

views:

5777

answers:

5

So far I've been using "public void run() {}" methods to execute my code in Java. When/why might one want to use main() or init() instead of run()?

+1  A: 

The main() method is the entry point for a Java application. run() is typically used for new threads or tasks.

Where have you been writing a run() method, what kind of application are you writing (e.g. Swing, AWT, console etc) and what's your development environment?

Jon Skeet
I'm working in Eclipse, and while I don't know what AWT or swing mean, I have certainly written console programs. All of my programs have so far only had 1 run() method and no main() methods.
Ziggy
Hmm... I've just tried in Eclipse, and I can't launch classes which just have a run() method. How are you running these programs?
Jon Skeet
+3  A: 

Java has a special static method:

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

which is executed in a class when the class is started with a java command line:

$ java Class

would execute said method in the class "Class" if it existed.

public void run() { ... }

is required by the Runnable interface, or inherited from the Thread class when creating new threads.

Matthew Schinckel
+12  A: 

This is a peculiar question because it's not supposed to be a matter of choice.

When you launch the JVM, you specify a class to run, and it is the main() of this class where your program starts.

By init(), I assume you mean the JApplet method. When an applet is launched in the browser, the init() method of the specified applet is executed as the first order of business.

By run(), I assume you mean the method of Runnable. This is the method invoked when a new thread is started.

  • main: program start
  • init: applet start
  • run: thread start

If Eclipse is running your run() method even though you have no main(), then it is doing something peculiar and non-standard, but not infeasible. Perhaps you should post a sample class that you've been running this way.

Jegschemesch
Ha ha ha, OK: I am importing files from libraries that I can't read. So probably there is a Runnable being created somewhere, and then my run() methods are running runables. This was great, thanks!
Ziggy
A: 

SUN Java tutorials

Please use them :)

And stick to main() for start.

public class HelloWorldApp {    
/** Creates a new instance of HelloWorldApp */
public HelloWorldApp() {
}    
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
}

}

Chobicus
+3  A: 

The main method is the entry point of a Java application.

Specifically、when the Java Virtual Machine is told to run an application by specifying its class (by using the java application launcher), it will look for the main method with the signature of public static void main(String[]).

From Sun's java command page:

The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method.

The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration must look like the following:

public static void main(String args[])

For additional resources on how an Java application is executed, please refer to the following sources:

  1. Chapter 12: Execution from the Java Language Specification, Third Edition.
  2. Chapter 5: Linking, Loading, Initializing from the Java Virtual Machine Specifications, Second Edition.
  3. A Closer Look at the "Hello World" Application from the Java Tutorials.


The run method is the entry point for a new Thread or an class implementing the Runnable interface. It is not called by the Java Virutal Machine when it is started up by the java command.

As a Thread or Runnable itself cannot be run directly by the Java Virtual Machine, so it must be invoked by the Thread.start() method. This can be accomplished by instantiating a Thread and calling its start method in the main method of the application:

public class MyRunnable implements Runnable
{
    public void run()
    {
     System.out.println("Hello World!");
    }

    public static void main(String[] args)
    {
     new Thread(new MyRunnable()).start();
    }
}

For more information and an example of how to start a subclass of Thread or a class implementing Runnable, see Defining and Starting a Thread from the Java Tutorials.


The init method is the first method called in an Applet or JApplet.

When an applet is loaded by the Java plugin of a browser or by an applet viewer, it will first call the Applet.init method. Any initializations that are required to use the applet should be executed here. After the init method is complete, the start method is called.

For more information about when the init method of an applet is called, please read about the lifecycle of an applet at The Life Cycle of an Applet from the Java Tutorials.

See also: How to Make Applets from the Java Tutorial.

coobird