2

I am learning core Java and I have one question, "Which is the first method called when the program is executed?"

skaffman
  • 398,947
  • 96
  • 818
  • 769
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • 1
    The static initisalizer for the class which contains the main is called first, only if this is successful does the main() method get called. – Peter Lawrey Nov 16 '10 at 13:42

5 Answers5

9

That would be the main method.

It should be declared as

public static void main(String[] args)
  • It needs to be public, since the JVM should have access to call the method.
  • It needs to be static, as no objects are instantiated when the program starts
  • It takes an array of Strings as argument (coming from the command-line)

Some good links to have a look at:


Some people may recommend you to write

public static void main(String... args)

this is equivalent to String[] args if you're using a compiler of version 1.5 or later. (I would discourage this unless you're extensively calling your main method internally with a varying number of arguments.)

Others may suggest

public static void main(String args[])

This is also equivalent, but discouraged by the Java Coding Convention.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • you missed var arg version of main check my answer http://stackoverflow.com/questions/4194407/which-is-the-first-method-called-when-java-executes-a-program/4194425#4194425 – jmj Nov 16 '10 at 13:19
5

It's usually main. But in this program, it's pain:

public class WhatThe {
    public static final int x = pain();
    public static int pain() {
        System.out.println("pain!");
        return 0;
    }
    public static void main(String[] args) {
        System.out.println("main");
    }
}

As it is in this one:

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

This is unlikely to be useful knowledge, but it's something to be aware of.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
4
public static void main(String ar[])

Java programs start executing at the main method, which has the following method heading:

public static void main(String[] args)
public static void main(String... args) //java 1.5+
public static void main(String args[])

Read more at docs

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Note that `String...` only works on Java 1.5+. Also, the `String args[]` syntax is discouraged by the Java Coding Convention. – aioobe Nov 16 '10 at 13:31
3

In addition to aioobes answer

A usual way to start a simple java program is to execute java like this:

java com.example.MyClass

com.example.MyClass (or your fully qualified class name) needs to have a main method with exactly this signature:

public static void main(String[] args)

(you're only allowed to change the name of the parameter, like arguments instead of args). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

The main method will be called first,control goes to main method first which has the method headings: public static void main(String[] args) or public static void main(String args[])

Preethi
  • 11
  • 1