I am learning core Java and I have one question, "Which is the first method called when the program is executed?"
-
1The 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 Answers
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
String
s as argument (coming from the command-line)
Some good links to have a look at:
- The main Method (from the official Getting started trail)
- What is the main method
- Entry point for Java applications: main(), init(), or run()?
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.
-
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
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.

- 46,189
- 17
- 92
- 133
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[])

- 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
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".

- 113,398
- 19
- 180
- 268
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[])

- 11
- 1