Support Online
Skip to main content

Starting Point of Java Programs: What is public static void main(String[] args)?

Keyword: public static void main
Meta Description: Learn each keyword of the public static void main(String[] args) structure in Java. Explore how the JVM launches the program, command line arguments, and common errors.

🎯 What Will You Learn in This Guide?

The main method by which the Java virtual machine (JVM) starts running an application is essential for every Java developer.
In this guide:

  • what each word in the public static void main(String[] args) structure does,
  • Why JVM is looking for this signature,
  • How you can use command line arguments,
  • And you'll learn how to solve the most common errors.

🧠 Technical Summary

CriterionDescription
Main Technical TopicFunction of the public static void main(String[] args) method, which is the starting point for program execution in Java.
Solved ProblemUnderstanding why the JVM looks for exactly this signature and the common mistakes developers make.
Technical SummaryThe main() method is the mandatory starting point of the Java program. Accessing public allows the JVM to see the method. static makes it possible to call the object without creating it. void indicates that no value will be returned. String[] args enables receiving data from outside.

Analysis of ## 🧩 public static void main(String[] args)

This signature is the contract between the JVM and your program. Only when JVM finds this method can it start the application.

1. public

It is an access token. Since the JVM is an external process, it needs to be public to call the method.

Otherwise:
Error: Main method not found in class Test

2. static

Indicates that the method belongs to the class.
Since the object has not been created yet when the application starts, the JVM calls the main() method directly through the class.

3. void

The method has no return type. When the program finishes, no value is returned to the JVM.
System.exit(0) can be used to terminate the program manually.

4. main

It is the standard name that JVM is looking for.
Recognized only as lowercase main; JVM gives error in different spellings.

5. String[] args

It carries the parameters entered from the command line.
For example:

java ProgramAdi param1 param2

Here args[0] = "param1", args[1] = "param2".


💻 Using Command Line Arguments

The following example prints the arguments entered at run time:


public class UygulamaBaslatma {

public static void main(String[] args) {
// args dizisindeki her bir argümanı ekrana yazdırır
for (String s : args) {
System.out.println("Gelen Argüman: " + s);
}
}
}

Operation:


javac UygulamaBaslatma.java
java UygulamaBaslatma tr1-node01 8080 "GenixNode"

Output:


Gelen Argüman: tr1-node01
Gelen Argüman: 8080
Gelen Argüman: GenixNode

🚨 Common Errors and Solutions

Error MessageReasonSolution
Main method not found...public, static or void is missing.Use full signature: public static void main(String[] args)
Main method is not static...The keyword static was forgotten.Add static between public and void.
ArrayIndexOutOfBoundsExceptionAn element like args[0] is accessed without entering any arguments.Check if (args.length > 0).
incompatible types: unexpected return valuereturn 0; used.Choose System.exit(0).

🧭 Best Apps

Keep the Main method simple: Manage complex business logic in other classes, not here.

Check the arguments: args.length and add security with try-catch.

Use separate initialization class: In large projects, define a separate entry class such as Initialization.java instead of Main.


❓ Frequently Asked Questions (FAQ)

  1. Is there flexibility in the main signature?

Yes. Different names can be used instead of args (String... args or String args[]). However, the order of public static void main should not change.

  1. Why should the main method be static?

JVM needs the static keyword to call the method without creating an object.

  1. Can there be more than one main in a class?

Yes, but only the one with the String[] args parameter is accepted as the JVM entry point.

  1. Can I use another entry point instead of main?

No. However, different lifecycle methods can be used in container environments such as web or Android.


🧩 Example Errors and Solutions

StatusError CodeCorrect Code
public missingstatic void main(...)public static void main(...)
static missingpublic void main(...)public static void main(...)
void wrongpublic static int main(...)public static void main(...)

🚀 Conclusion

The public static void main(String[] args) structure is the mandatory starting point of Java programs. Each word has a specific role for the JVM to launch the application.

When you understand this structure correctly, you lay the foundations for developing robust, scalable and error-free applications in Java.

💡 You can immediately test these concepts on the GenixNode platform and run your own Java application in the cloud!