h1

main() Methods in Java

January 29, 2007

Introduction

The main() method is probably one of the most familar structures in the Java language. It can easily be auto-generated in Eclipse by typing “main” followed by Ctrl-Space and in NetBeans by typing “psvm” followed by a space. It’s the entry point into thousands of applications and while it’s familiar, it also has a few hidden secrets. In this article we’ll look at more than a dozen variations of the main() method. Some will compile and run as expected. Others will not compile at all. Still others will compile and run, but can’t be used as an entry point into an application.

The Methods

Look at the methods below. Which ones will not compile? Which ones will compile, but can’t be used as entry points into an application? Which ones compile and act as you would expect a main method to act?


public static void main(String[] args) {
    System.out.println("Main1!");
}

public static void main(String[] someOtherName) {
    System.out.println("Main2!");
}

public static void main(String... args) {
    System.out.println("Main3!");
}

public static void main(String[] args)
  throws Exception {
    System.out.println("Main4!");
}

static void main(String[] args) {
    System.out.println("Main5!");
}

public void main(String[] args) {
    System.out.println("Main6!");
}

public static void main(String args[]) {
    System.out.println("Main7!");
}

public static void main(String[] args[]) {
    System.out.println("Main8!");
}

public static void main(String[][] args) {
    System.out.println("Main9!");
}

public static void main(String args) {
    System.out.println("Main10!");
}

public static void main(String[] args)
  throws IOException {
    System.out.println("Main11!");
}

static public void main(String[] args) {
    System.out.println("Main12!");
}

public strictfp static void main(String[] args) {
    System.out.println("Main13!");
}

void public static main(String[] args) {
    System.out.println("Main14!");
}

public static void main(int[] args) {
    System.out.println("Main15!");
}

public static void main(String[] args) {
    System.out.println("Main16!");
}

public static void Main(String[] args) {
    System.out.println("Main17!");
}
    

The Answers


    
/**
 * Fine.
 * 
 * This is the most common form of the main method.
 */
public static void main(String[] args) {
    System.out.println("Main1!");
}

/**
 * Fine.
 * 
 * This is the most common form of the main method except
 * that the variable accepting command line arguments has
 * been renamed to someOtherName. The name of the variable
 * is insignificant.
 */
public static void main(String[] someOtherName) {
    System.out.println("Main2!");
}

/**
 * Fine.
 * 
 * Varargs form of the main method. New with Java 5.
 */
public static void main(String... args) {
    System.out.println("Main3!");
}

/**
 * Fine.
 * 
 * This is the most common form of the main method, except
 * that it throws an exception. This is completely valid.
 */
public static void main(String[] args)
  throws Exception {
    System.out.println("Main4!");
}

/**
 * Compiles, but cannot be executed from the command line.
 * 
 * Method must be public.
 * 
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 * 
 */
static void main(String[] args) {
    System.out.println("Main5!");
}

/**
 * Compiles, but cannot be executed from the command line.
 * 
 * Method must be static.
 * 
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 * 
 */
public void main(String[] args) {
    System.out.println("Main6!");
}

/**
 * Fine.
 * 
 * This is the most common form of the main method
 * except that the square
 * brackets for the String array have been put beside
 * the variable. This is valid, but many think, harder
 * to read.
 */
public static void main(String args[]) {
    System.out.println("Main7!");
}

/**
 * Although the syntax is strange, this compiles, but
 * cannot be executed from the command line.
 *
 * This is the most common form of the main method, but
 * the square brackets for the args array are beside the
 * type as well as beside the args variable. They should
 * be beside one or the other, not both.
 *
 * While I would have guessed that this would not
 * compile at all, it turns out that this is equivalent
 * to main taking a two-dimensional array as a parameter.
 * String[] args[] is the same as String[][] args or String
 * args[][]. While it's certainly valid for a method to
 * accept a two-dimensional array of Strings, this does
 * not fit the required signature for a main method
 * that is to be invoked from the command line. Attempting
 * to execute this will result in the following error
 * message: Exception in thread "main"
 * java.lang.NoSuchMethodError: main
 *
 */
public static void main(String[] args[]) {
    System.out.println("Main8!");
}

/**
 * Compiles, but cannot be executed from the command line.
 *
 * The main() method needs to accept an array of Strings
 * as a parameter. The method below accepts a
 * two-dimensional array of Strings.
 *
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 *
 */
public static void main(String[][] args) {
    System.out.println("Main9!");
}

/**
 * Compiles, but cannot be executed from the command
 * line.
 *
 * The main() method needs to accept an array of Strings
 * as a parameter. The method below accepts a single
 * String called args.
 *
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 *
 */
public static void main(String args) {
    System.out.println("Main10!");
}

/**
 * Fine.
 * 
 * Throwing a checked exception, like IOException,
 * is legal.
 * 
 */
public static void main(String[] args)
  throws IOException {
    System.out.println("Main11!");
}

/**
 * Fine.
 * 
 * This is the most common form of the main method except
 * that static and public keywords are reversed. Their
 * order does not matter.
 * 
 */
 static public void main(String[] args) {
    System.out.println("Main12!");
}

/**
 * Fine.
 * 
 * It's perfectly acceptable to have a strictfp main
 * method.
 * 
 */
public strictfp static void main(String[] args) {
    System.out.println("Main13!");
}

/**
 * Does not compile.
 * 
 * The return type (void in this case) must come
 * immediately before the method name.
 * 
 */
 void public static main(String[] args) {
    System.out.println("Main14!");
}

/**
 * Compiles, but cannot be run from the command line.
 *
 * The main() method must accept an array of Strings,
 * not ints, as a parameter.
 *
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 *
 */
public static void main(int[] args) {
    System.out.println("Main15!");
}

/**
 * Fine.
 * 
 * This is the most common form of the main method
 * except that there aren't any spaces between the
 * type, the square brackets and the variable name.
 * It's still legal.
 *
 */
public static void main(String[] args) {
    System.out.println("Main16!");
}

/**
 * Compiles, but cannot be run from the command line.
 *
 * The main() method must be all lower case.
 *
 * Since the signature doesn't match it's a completely
 * different method that just happens to be called main.
 *
 */
public static void Main(String[] args) {
    System.out.println("Main17!");
}

So why would you want to know so much about the main() method anyway? Well, besides being essential if you’re taking the Sun Certified Java Programmer (SCJP) exam, there are a few alternate formulations of the method that might come in handy. Plus, with the examples above it mind, I bet you’ll spot a flawed main() method quicker than most if you come across one.

Joshua Smith


References


SCJP Sun Certified Java Programmer Study Guide

Technorati Tags: ,

About these ads

19 comments

  1. Why the return type of the main method is void?


  2. Where’s it going to return to? It’s called off the command line.


  3. MR.Joshua Smith,
    Thanks for the reply, But if we mention the return type to main method, it compiles without any err. but while running the same it throws exception. Why?Please explain.


  4. If you specify a return type for the main method, it’s just another method that happens to be called main. It compiles fine and that’s ok.

    The problem is that if you try to invoke the class from the command line, it will not find the method because it doesn’t have the appropriate signature. Main, with a specific signature, and is found and executed off the command line.

    What is the exception that you’re seeing?

    Josh


  5. Hi Mr. Josh,
    Im getting this Exception:
    ‘Exception in thread “main” lang.NoSuchMethodError: main’

    Please explain me with detail what each signifies in

    public static void main(String [] ar)

    Regards,
    Nandan


  6. An informative, thorough and amusing read. Thanks for investigating this and sharing it on your web site. Once live online, I will be linking my blog http://dichotunity.blogspot.com/ to it as I believe your coverage of the topic is not just comprehensive but exhaustive! :-)


  7. what is the return type of the println() method ?

    plz reply me on dileep252000@gmail.com


  8. public static void main(String agrs[])

    public means ant object can access it.

    static means it cannot be overidden

    void means it will not return anything.

    main — method name


  9. The static keyword in Java means that the method or field belongs to the class and not to an instance of a class.


  10. Hi Mr.Josh,

    i want to know 2 things deeply
    1) why main method only return public why not it is
    private,default,protected
    2) why is only take String array as parameter except any other reference type or primitive type


  11. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.


  12. Sign: zdbrw Hello!!! rwzgr and 5199kzhpmdnxvb and 4136 : I will try to recommend this post to my friends and family, cuz its really helpful.


  13. Hi MR.Joshua Smith
    My question is,
    public static void main(String ag[]) throws Exception{
    throwa new Exception();
    }
    Please explain it in detail.


  14. My question is this “we know very well the main() method in java but why jvm reqiured main method and from which class or interface this belong to, is that method of object class?,so jvm need this main() to run the program”


  15. Just other examples that compile but don’t get executed:

    public static void main(Object args){}
    public static void main(Object[] args){}
    public static void main(CharSequence[] args){}

    So I guess the only valid argument types are “String[]” or “String…”


  16. Hi,

    I am new to programing and I want to learn more about Java.
    Here are my questions:

    1. Can there be more than 1 main method in a program?
    2. What are the best practices to build and use the main method?

    Thank you!


  17. can any one help me
    how the main method work internally in a program
    ?
    how console move from line by line and it returns back ti main method only?


  18. Good one


  19. thanks for putting so many versions of main method. those preparing for SCJP/OCPJCP, keep in mind that this question is very frequently asked.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: