main()
Method in Java
The main()
method is the entry point for Java applications. It is where execution begins when you run a Java program.
1. main()
Method Signature
public static void main(String[] args)
Each part of the method signature has a specific purpose:
Keyword | Meaning |
---|---|
public | The method is accessible from anywhere (must be public so JVM can call it). |
static | Allows main() to run without creating an object of the class. |
void | main() does not return any value. |
main | This is the method name that the JVM searches for as the starting point. |
String[] args | An array of command-line arguments passed to the program. |
2. Why Must main()
Be public static
?
public
→ The JVM must be able to callmain()
from outside the class.static
→ The JVM does not need to create an object of the class to executemain()
.
If main()
were not static, the JVM would need to create an instance before calling it, but without main()
, it wouldn’t know how!
3. Can We Change the main()
Method?
✅ Valid Variations of main()
The following variations work because they retain the correct method signature:
✔ Different parameter name
public static void main(String[] arguments) { }
✔ Varargs instead of array
public static void main(String... args) { }
✔ Final modifier (allowed but unnecessary)
public static final void main(String[] args) { }
❌ Invalid main()
Methods
These will not be recognized by the JVM:
❌ Missing static
(JVM doesn’t know how to call it)
public void main(String[] args) { }
❌ Wrong parameter type (must be String[]
)
public static void main(int[] args) { }
❌ Wrong return type (main()
must be void
)
public static int main(String[] args) { return 0; }
4. Command-Line Arguments (args
)
You can pass arguments to main()
from the command line.
Example: Using args
public class CommandLineExample {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
for (String arg : args) {
System.out.println("Argument: " + arg);
}
}
}
Run in Terminal:
java CommandLineExample Hello World 123
Output:
Number of arguments: 3
Argument: Hello
Argument: World
Argument: 123
5. main()
in Multi-Threaded and GUI Applications
Even in Java applications with multiple threads or GUI interfaces, main()
still serves as the starting point.
✔ In multi-threading, main()
launches other threads:
public class MultiThreadExample {
public static void main(String[] args) {
Thread t1 = new Thread(() -> System.out.println("Thread running"));
t1.start();
}
}
✔ In Swing/GUI apps, main()
launches the UI:
import javax.swing.*;
public class GUIExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Hello");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
6. Can We Have Multiple main()
Methods?
✔ Yes! You can have multiple main()
methods in different classes, but only one main entry point when running the program.
class A {
public static void main(String[] args) {
System.out.println("Main in A");
}
}
class B {
public static void main(String[] args) {
System.out.println("Main in B");
}
}
Run Command:
java A
Output:
Main in A
Only the specified class’s main()
runs.
7. Can We Call main()
Manually?
Yes, you can call main()
like a normal method:
public class MainCall {
public static void main(String[] args) {
System.out.println("Main method called!");
main(new String[]{"Hello", "World"}); // Recursively calling main()
}
}
✔ Be careful! Recursive calls to main()
can cause a StackOverflowError
.
Final Thoughts
✔ main()
is the entry point for Java programs.
✔ It must be public static void main(String[] args)
.
✔ You can pass command-line arguments using args
.
✔ GUI and multi-threaded applications still need main()
to start execution.
✔ You can’t change the method signature, but variations (like String... args
) are allowed.