Skip to main content

🔹 Java main() Method Syntax – Explained in Simple Words || FSJD-09-29-05

🔹 Java main() Method Syntax – Explained in Simple Words

"An investment in knowledge pays the best interest." — Benjamin Franklin

In this guide, we’ll explore different ways the main() method can be written in Java. You might see various syntaxes — some valid, some invalid, and some that look okay but are not suitable as the main method. Let's simplify it all here.


✅ What is the main() Method?

In Java, the main() method is the starting point of every program. It tells the Java Virtual Machine (JVM) where to begin executing your code.

The standard syntax looks like this:

public static void main(String[] args)

Let’s break it down:

Keyword Meaning
public Accessible from anywhere
static No object is needed to call it
void It doesn’t return any value
main The method name recognized by JVM to start execution
String[] args Accepts command-line arguments in the form of a string array

✅ Different Valid and Invalid main() Method Syntaxes

Let’s go one-by-one with easy explanations for each variation you might encounter.


✅ Valid main() Method Variations

All of these are allowed in Java, and your program will run successfully:

public static void main(String[] args)
static public void main(String[] args)

✔ Changing the order of public and static is allowed.

public static void main(String args[])

✔ You can place [] after the variable instead of after the type.

public static void main(String []args)
public static void main(String[ ] args)```

```java
public static void main(String[] xyz)

✔ You can change the name args to anything — it's just a variable name.

public static void main(String... args)

✔ This is called a varargs version. It works the same as String[] args. But remember:
Only 3 dots (...) are allowed, no more or less.

final public static void main(String[] args)
synchronized public static void main(String[] args)
strictfp public static void main(String[] args)

✔ All above are legal — adding modifiers like final, synchronized, or strictfp is allowed.


❌ Incorrect or Invalid main() Methods

These examples may look fine but will not work as the main() method — either they are wrong for JVM or just wrong syntax altogether.


public void static main(String[] args)

❌ Invalid: You must write returnType (void) before the method name.
🛑 JVM will throw a compile-time error: “Method name expected after return type”


public static void args(String[] args)
public static void xyz(String[] args)

❌ Invalid as main method:
✔ These are valid custom methods, but JVM will not consider them as the starting point because the method name is not main.


public static void main(Integer[] args)

❌ Invalid as main method:
✔ This is valid method syntax but JVM requires String[] as argument. Not Integer[].


protected static void main(String[] args)
private static void main(String[] args)

❌ Invalid as main method:
✔ These are valid Java methods, but the JVM needs the method to be public to access it from outside the class.


public static void main(String args() {}) 

❌ Invalid: Wrong use of syntax. args() is treated as a method or function.


public static void main(String args)

❌ Invalid for main():
✔ Valid Java method, but again, JVM expects a String[] argument, not just a String.


public void main(String[] args)
static void main(String[] args)

❌ Invalid as main() method:
✔ JVM requires main() to be both public and static. These will compile but will throw runtime error:
Main method not found in class Test...


🧠 Recap Table: Summary of Validity

Syntax Variation Valid main()? Notes
public static void main(String[] args) ✅ Standard main method
static public void main(String[] args) ✅ Order doesn't matter
public static void main(String args[]) ✅ [] after variable is okay
public static void main(String... args) ✅ Varargs supported
public static void args(String[] args) ❌ Not named main()
public void static main(String[] args) ❌ Wrong order
private static void main(String[] args) ❌ Must be public
public static void main(Integer[] args) ❌ Wrong parameter type
public static void main(String args) ❌ Needs to be an array
public static void main(String args() {}) ❌ Totally invalid syntax
public void main(String[] args) ❌ Not static
static void main(String[] args) ❌ Not public

🔚 Final Thoughts

  • The main method must follow strict syntax for the JVM to recognize and execute it.

  • You can tweak some parts like the argument variable name, use of [], or keyword order.

  • Avoid incorrect modifiers or return types that prevent execution.

📘 Pro Tip: Always use the standard form for interviews and real-world development:

public static void main(String[] args)


Comments

Popular posts from this blog

What is Java? A Complete Beginner Guide to Java as a Language and Platform || FSJD-01-24-05

☕ What is Java? A Beginner-Friendly Guide to Java as a Language and Platform Are you just stepping into the world of programming? Or preparing for that first tech interview and trying to make sense of Java? Don’t worry — you're in the right place. Java isn’t just a coding language; it’s a whole ecosystem. In this beginner-friendly post, we’ll break down what Java really is — in simple terms — so you can start your learning journey with clarity and confidence. 🧠 So… What Exactly is Java? Java is both a programming language AND a platform . Yes — it’s more than just lines of code. Java provides a complete environment that lets you build applications for almost any device — from laptops to smartphones to smart TVs and more. 💻 Java as a Programming Language Java was created by James Gosling and his team at Sun Microsystems (now owned by Oracle ) and officially released in 1995 . Let’s break down what makes Java special as a language: ✅ High-Level Easy to read and wr...

☕ Understanding the Structure of a Java Program – Documentation || FSJD-07-29-05

☕ Understanding the Structure of a Java Program – A Beginner to Pro Guide “An investment in knowledge pays the best interest.” — Benjamin Franklin If you’re starting your journey in Java, the structure of a Java program is one of the first and most important things you should understand. Whether you're preparing for interviews or writing your first piece of code, a strong foundation can set you up for success. In this blog, we’ll break down the complete anatomy of a Java program — from the documentation section to the main() method — and explain each component in simple yet professional terms. 📋 Step-by-Step: How to Prepare Your First Java Program Before writing a Java program, follow these key steps: Download & Install Java Software Install the Java Development Kit (JDK) Choose an editor or IDE (like VS Code , IntelliJ , or Eclipse ) Understand the Java Program Structure Learn Java naming conventions Understand different sections o...

🔰 Java Software Setup Guide for Beginners || FSJD-05-29-05

  🔰 Java Software Setup Guide for Beginners By: Tahir | For Interview Prep & Fresh Learning “An investment in knowledge pays the best interest.” – Benjamin Franklin Whether you’re starting out with Java or brushing up for interviews, setting up the right environment is your first and most important step. In this post, I’ll walk you through the essential tools and software needed to start coding in Java, using simple language and clear concepts. 🚀 What Do You Need to Start Java Programming? To begin Java development, you need three main things: 1. JDK (Java Development Kit) The JDK is the core software you need to write, compile, and run Java programs. ✅ What Is JDK? The Java Development Kit is a package of tools that allows developers to write Java programs. It includes: A compiler ( javac ) to convert Java code into bytecode The Java Runtime Environment (JRE) to run the bytecode Development tools and libraries 📌 Important JDK Versions: Java S...