Java Programming Language
Table of content:
- History Of Java Programming Langauge
- Infographic For History Of Java
- What’s In The Name | History Of Java
- Key Features Of Java
- Advantages And Disadvantages Of Java
- The Version History Of Java Langauge
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is JDK?
- How To Download Java Development Kit (JDK) For Windows, MacOS, and Linux?
- Set Environment Variables In Java
- How To Install Java (JDK) On Windows 64-Bit Machine?
- How To Install Java (JDK) On Linux?
- How To Install Java (JDK) On macOS?
- How To Test Java Installation?
- How To Write Your First Java Program On Linux OS?
- Conclusion
- Frequently Asked Questions
Table of content:
- Java Programming Language | An Introduction
- 15 Key Features Of Java
- Write Once Run Anywhere (WORA) | Features Of Java
- Java Editions
- 5 New Features Of JAVA 8
- 5 New Features Of JAVA 11
- What Makes Java Popular?
- Conclusion
- Frequently Asked Questions
Table of content:
- What is Java?
- Advantages of Java
- Disadvantages of Java
Table of content:
- What Is Java Programming?
- Role Of Integrated Development Environments (IDEs) In Java Development
- 15 Best Java IDE For Developers
- In-Depth Comparison Table
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences Between Java And Core Java
- What Is Java?
- What Is Core Java?
- Applications Of Java
- Applications Of Core Java
- When To Use Java?
- When To Use Core Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Variables In Java Language?
- How To Declare Variables In Java Programs?
- How To Initialize Variables In Java?
- Naming Conventions For Variables In Java
- Types Of Variables In Java
- Local Variables In Java
- Instance Variables In Java
- Static Variables In Java
- Final Variables In Java
- Scope and Lifetime of Variables In Java
- Data Types Of Variables In Java (Primitive & Non-primitive)
- Java Variable Type Conversion & Type Casting
- Working With Variables In Java (Examples)
- Access Modifiers & Variables In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Identifiers In Java?
- Syntax Rules For Identifiers In Java
- Valid Identifiers in Java
- Invalid Identifiers in Java
- Java Reserved Keywords
- Naming Conventions & Best Practices For Identifiers In Java
- What Is An Identifier Expected Error In Java?
- Reasons The Identifier Expected Error Occurs
- How To Fix/ Resolve Identifier Expected Errors In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Data Types In Java?
- Primitive Data Types In Java
- Non-Primitive Data Types In Java
- Key Differences Between Primitive And Non-Primitive Data Types In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Operators In Java?
- Types Of Operators In Java
- Unary Operators In Java
- Arithmetic Operators In Java
- Assignment Operators In Java
- Relational Operators In Java
- Logical Operators In Java
- Bitwise Operators In Java
- Shift Operators In Java
- Increment & Decrement Operators In Java
- Ternary Operator In Java
- Instanceof Operator In Java
- Precedence & Associativity Of Java Operators
- Advantages & Disadvantages Of Operators In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Return Statement In Java?
- Use Cases Of Return Statements In Java
- Returning A Value From A Method In Java
- Returning A Class Object In Java
- Returning Void (No Value) In Java
- Advantages Of Using Return Statements In Java
- Limitations Of Using Return Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Keywords In Java?
- List Of Keywords In Java
- Detailed Overview Of Java Keywords With Examples
- What If When Keywords In Java Are Used As Variable Names?
- Difference Between Identifiers & Keywords In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Abstract Keyword In Java?
- Use Of Abstract Keyword In Java
- Abstract Methods In Java
- Abstract Classes In Java
- Advantages Of Abstract Keyword In Java
- Disadvantages Of Abstract Keyword In Java
- Abstract Classes Vs. Interfaces In Java
- Real-World Applications Of Abstract Keyword
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is throws Keyword In Java?
- How Does The throws Keyword Work?
- Throwing A Checked Exception Using throws In Java
- Throwing Multiple Exceptions Using throws In Java
- Throwing A Custom Exception Using throws In Java
- When To Use The throws Keyword In Java
- Difference Between throw and throws Keyword In Java
- Best Practices For Using The throws Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Volatile Keyword In Java?
- How Does Volatile Keyword In Java Work?
- Using Volatile Keyword In Java To Control Thread Execution
- Using Volatile Keyword In Java To Signal Between Multiple Threads
- Difference Between Synchronization And Volatile Keyword
- Common Mistakes And Best Practices While Using Volatile Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Super Keyword In Java
- Super Keyword In Java With Instance Variables
- Super Keyword In Java With Method Overriding
- Super Keyword In Java With Constructor Chaining
- Applications Of Super Keyword In Java
- Difference Between This And Super Keyword In Java
- Advantages Of Using Super Keyword In Java
- Limitations And Considerations Of Super Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding This Keyword In Java
- Uses Of This Keyword In Java
- Using This Keyword For Referencing Instance Variables
- Using This Keyword For Invoking A Constructor
- Using This Keyword For Invoking A Method
- Using This Keyword With Getters And Setters
- Difference Between This And Super Keyword In Java
- Best Practices For Using This Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is new Keyword In Java?
- Uses Of The new Keyword In Java
- Memory Management With new Keyword In Java
- Example 1: Creating An Object Of A Class Using new Keyword In Java
- Example 2: Creating An Array Using The new Keyword In Java
- Best Practices For Using new Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Transient Keyword In Java?
- Real-Life Example Of The Transient Keyword In Java
- When To Use The Transient Keyword In Java
- Example 1: Effect Of Transient Keyword On Serialization In Java
- Example 2: Skipping Sensitive Data During Serialization With Transient Keyword In Java
- Using Transient With Final Keyword In Java
- Using Transient With Static Keyword
- Difference Between Transient And Volatile Keyword In Java
- Advantages And Disadvantages Of Transient Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Static Keyword In Java?
- Characteristics Of Static Keyword In Java
- Static Variables In Java
- Static Method In Java
- Static Blocks In Java
- Static Classes In Java
- Static Variables Vs Instance Variables In Java
- Advantages Of Static Keyword In Java
- Disadvantages Of Static Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Static Method In Java?
- Use Cases Of Static Method In Java
- Using Static Method In Java To Create A Utility Class
- Using Static Method In Java To Implement The Singleton Design Pattern
- Difference Between Static And Instance Methods In Java
- Limitations Of Static Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Final Keyword In Java
- Final Variables In Java
- Final Methods In Java
- Final Classes In Java
- Difference Between Static And Final Keyword In Java
- Uses Of Final Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Difference Between final, finally, And finalize In Java
- What Is final Keyword In Java?
- What Is finally Keyword In Java?
- What Is finalize Keyword In Java?
- When To Use Which Keyword In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The extends Keyword In Java?
- Use Of extends Keyword In Java
- Using Java extends To Implement Single Inheritance
- Using Java extends With Interfaces (Default Methods)
- Overriding Using extends Keyword In Java
- Difference Between extends And implements In Java
- Real World Applications Of Extends Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Decision Making Statement In Java?
- If Statement In Java
- If-Else Statement In Java
- Else-If Ladder In Java
- Switch Statement In Java
- Ternary/Conditional Operator (?:) In Java
- Best Practices For Writing Decision Making Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Control Statements in Java?
- Decision-Making Control Statements In Java
- Looping Control Statements In Java
- Jump (Branching) Control Statements In Java
- Application Of Control Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Break Statement In Java?
- Working Of The Break Statement In Java
- Using Java Break Statement With Loops
- Using Java Break Statement With Switch Statement
- Using Java Break Statement With Infinite Loops
- Common Pitfalls While Using Break Statements In Java
- Best Practices For Using The Break Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Switch Statement In Java?
- Working Of The Switch Statement In Java
- Example Of Switch Statement In Java
- Java Switch Statement With String
- Java Nested Switch Statements
- Java Enum In Switch Statement
- Java Wrapper Classes In Switch Statements
- Uses Of Switch Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Syntax Of main() Method In Java
- public Specifier – Main Method In Java
- static Keyword – Main Method In Java
- void Return Type Of Main Method In Java
- The main Identifier – Main Method In Java
- String[] args In Main Method In Java
- The Role Of Java Virtual Machine (JVM)
- Running Java Programs Without The Main Method
- Variations In Declaration Of Main Method In Java
- Overloading The Main Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overriding In Java?
- Example Of Method Overriding In Java
- Ideal Use Cases Of Method Overriding In Java
- Rules For Method Overriding In Java
- Super Keyword & Method Overriding In Java
- Constructor & Method Overriding In Java
- Exception Handling In Method Overriding In Java
- Access Modifiers In Method Overriding In Java
- Advantages & Disadvantages Of Method Overriding In Java
- Difference Between Method Overloading Vs. Method Overriding In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overloading In Java?
- Different Ways Of Method Overloading In Java
- Overloading The main() Method In Java
- Type Promotion & Method Overloading In Java
- Null Error & Method Overloading In Java
- Advantages Of Method Overloading In Java
- Disadvantages Of Method Overloading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Overloading And Overriding In Java (Comparison Table)
- What Is Method Overloading In Java?
- What Is Method Overriding In Java?
- Key Differences Between Overloading & Overriding In Java Explained
- Difference Between Overloading And Overriding In Java Code Example
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A One-Dimensional Array In Java?
- Key Characteristics Of One-Dimensional Arrays In Java
- Declaration Of One-Dimensional Array In Java
- Initialization Of One-Dimensional Array In Java
- Common Operations On One-Dimensional Array In Java
- Advantages Of One-Dimensional Arrays In Java
- Disadvantages Of One-Dimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Multidimensional Array In Java?
- Difference Between Single-Dimensional And Multidimensional Arrays In Java
- Declaring Multidimensional Arrays In Java
- Initializing Multidimensional Arrays In Java
- Accessing And Manipulating Elements In Multidimensional Arrays In Java
- Working Of Multidimensional Arrays With Jagged Arrays In Java
- Why Use Multidimensional Arrays In Java?
- Limitations Of Multidimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Jagged Arrays In Java?
- Comparison With Regular Multi-Dimensional Arrays
- Declaring Jagged Arrays In Java
- Initialization Of Jagged Arrays In Java
- Printing Elements Of A Jagged Array In Java
- Accessing And Modifying Elements Of A Jagged Array In Java
- Advantages Of Jagged Arrays In Java
- Disadvantages Of Jagged Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Array Of Objects In Java?
- Declare And Initialize An Array Of Object In Java
- Example Of An Array Of Objects In Java
- Sorting An Array Of Objects In Java
- Passing Arrays Of Objects To Methods In Java
- Returning Arrays Of Objects From Methods In Java
- Advantages Of Arrays Of Objects In Java
- Disadvantages Of Arrays Of Objects In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Dynamic Array In Java?
- Why Use Dynamic Array In Java?
- What Is The Size And Capacity Of A Dynamic Array In Java?
- How To Create A Dynamic Array In Java?
- Managing Dynamic Data Input In Java
- Storing And Processing Real-Time Data In Java
- Use Cases Of Dynamic Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Return An Array In Java?
- How To Return An Array In Java
- Example 1: Returning An Array Of First N Squares
- Example 2: Doubling the Values of an Array
- Common Scenarios For Returning Arrays In Java
- Points To Remember When Returning Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding ArrayList In Java
- Differences Between Arrays And ArrayList In Java
- Returning An ArrayList In Java
- Common Use Cases For Returning An ArrayList In Java
- Pitfalls To Avoid When Returning An ArrayList In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Thread In Java?
- Thread Vs Process
- What is a Thread Life Cycle In Java?
- What Are Thread Priorities?
- Creating Threads In Java
- Java Thread Methods
- Commonly Used Constructors In Thread Class
- Thread Synchronization In Java
- Common Challenges Faced While Using Threads In Java
- Best Practices For Using Threads In Java
- Real-World Applications Of Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Multithreading In Java
- Methods Of Multithreading In Java (Examples)
- Difference Between Multithreading And Multitasking In Java
- Handling Exceptions In Multithreading
- Best Practices For Multithreading In Java
- Real-World Use Cases of Multithreading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Priority In Java?
- Built-In Thread Priority Constants In Java
- Thread Priority: Setter & Getter Methods
- Limitations Of Thread Priority In Java
- Best Practices For Using Thread Priority In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Synchronization In Java?
- The Need For Thread Synchronization In Java
- Types Of Thread Synchronization In Java
- Mutual Exclusion In Thread Synchronization In Java
- Coordination Synchronization (Thread Communication) In Java
- Advantages Of Thread Synchronization In Java
- Disadvantages Of Thread Synchronization In Java
- Alternatives To Synchronization In Java
- Deadlock And Thread Synchronization In Java
- Real-World Use Cases Of Thread Synchronization In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Daemon Thread In Java?
- User Threads Vs. Daemon Threads In Java
- Methods For Daemon Threads In The Thread Class
- Creating Daemon Threads In Java
- Checking The Daemon Status Of A Thread
- Exceptions In Daemon Threads
- Limitations Of Daemon Threads In Java
- Practical Applications Of Daemon Threads In Java
- Common Mistakes To Avoid When Working With Daemon Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Do Threads Need To Communicate?
- Understanding Inter Thread Communication In Java
- The wait() Method In Inter-Thread Communication
- The notify() Method In Inter-Thread Communication
- The notifyAll() Method In Inter-Thread Communication
- Difference Between wait() And sleep() Methods In Java
- Best Practices For Inter Thread Communication In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Factorial Concept
- Approaches To Implementing Factorial In Java
- Find Factorial In Java Using Iterative Approach (Using a Loop)
- Find Factorial In Java Using Recursive Approach
- Complexity Analysis Of Factorial Programs In Java
- Applications Of Factorial Program In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Leap Year Concept
- Approach To Check A Leap Year In Java
- Alternative Approach To Check A Leap Year In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Difference Between JDK, JRE, and JVM?
- What Is JVM (Java Virtual Machine)?
- What Is JRE (Java Runtime Environment)?
- What Is JDK (Java Development Kit)?
- Understanding The Difference Between JDK, JRE, And JVM
- Comparison Table For Difference Between JDK, JRE, And JVM
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Abstraction And Encapsulation In Java
- Understanding Abstraction In Java
- Understanding Encapsulation In Java
- When To Use Abstraction And Encapsulation?
- Conclusion
- Frequently Asked Questions
Table of content:
- Differences Between Abstract Class And Interface In Java
- What Is An Abstract Class In Java?
- What Is An Interface In Java?
- When To Use An Abstract Class?
- When To Use Interface?
- Compatibility Between Abstract Class And Interface In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Error Vs. Exception In Java
- What Is Error In Java?
- What Is Exception In Java?
- Best Practices For Handling Exceptions In Java
- Why Errors Should Not Be Handled In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences: Java Vs. JavaScript
- What Is Java?
- What Is JavaScript?
- Difference Between Java And JavaScript Explained
- Conclusion
- Frequently Asked Questions
Table of content:
- Brief Introduction To C++
- Brief Introduction To Java
- Difference Between C++ and Java
- Overview & Features Of C++ Language
- Overview & Features of Java Language
- Example of C++ and Java Program
- Key Difference Between C++ And Java Explained
- Similarities Between Java Vs. C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Basic Java interview questions and answers
- Intermediate Java interview questions and answers
- Advanced Java interview questions and answers
Table of content:
- Difference between core Java and advanced Java
- Important Core Java Questions
- Tips for Preparing for Core Java
Main Method In Java | Breakdown, Rules & Variations (+Examples)

Imagine writing a heartfelt letter but forgetting to mention the recipient’s name. No matter how well-written it is, no one knows whom it’s for! That’s exactly how Java programs feel without the main() method—confused and directionless.
The main() method in Java is where the magic begins. It's the official entry point for any standalone Java application, acting as the bridge between your code and the Java Virtual Machine (JVM). Without it, your program won’t execute, no matter how brilliant your logic is.
In this article, we’ll break down the main() method in Java, its structure/components, and more.
What Is main() Method In Java?
The main() method in Java is the designated entry point for program execution. When you run a Java application, the Java Virtual Machine (JVM) searches for this method to kick-start the execution.
Key Points About Java main() Method:
- It must be public, static, and void to be recognized by the JVM.
- It is placed inside a class since Java follows an object-oriented approach.
- The parameter String[] args allows command-line arguments to be passed when executing the program.
- Without a proper main() method, your Java application won’t run successfully.
For example, a simple Java program always includes the main() method:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Importance Of main() Method In Java Applications
- Program Entry Point: The main() method is the first thing the JVM looks for when running a program.
- Standard Convention: Following this structure ensures consistency across Java applications.
- Command-Line Interaction: The String[] args parameter enables command-line arguments for flexibility.
- Foundation for Larger Programs: Even complex Java applications start with main() as an entry point before branching into other methods and classes.
Syntax Of main() Method In Java
The main() method in Java programming language follows a specific structure that the JVM recognizes to start program execution. Here’s its standard syntax:
public static void main(String[] args) {
// Code execution starts here
}
Let's break down its components:
Component |
Meaning |
public |
Makes the method accessible to the JVM from anywhere. |
static |
Allows JVM to call main() without creating an object. |
void |
Specifies that the method doesn’t return any value. |
main |
The predefined name is recognized by the JVM as the entry point. |
String[] args |
Accepts command-line arguments as an array of strings. |
Look at the labeled diagram of the syntax of the main() method in Java, given in the banner image at the top, to get a better idea. Each component plays a crucial role, and we will discuss each of these in detail in the following sections.
public Specifier – Main Method In Java
An access specifier determines the visibility of a method or variables in Java programs. The main() method uses the public access specifier, ensuring that it is accessible to the Java Virtual Machine (JVM) from anywhere. Since the JVM needs to call main() from outside the class, making it public is essential.
Why Is main() Public?
- JVM Accessibility: Since the JVM calls main(), it must be publicly accessible from outside the class.
- Prevents Access Issues: If main() were private or protected, the JVM wouldn’t be able to execute it, leading to an error.
- Standard Convention: Java follows strict conventions, and making main() public ensures uniformity across all applications.
What Happens If main() Is Not Public?
Let’s see an example where main() is declared as private (filename– Demo.java):
class Demo {
private static void main(String[] args) {
System.out.println("This won't work!");
}
}
Error Output:
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
Explanation:
- Since main() is private, the JVM cannot access it, resulting in an error.
- To fix this, we must declare main() as public.
static Keyword – Main Method In Java
The static keyword in Java allows a method or variable to belong to the class itself rather than requiring an instance of the class. In the main() method, static plays a crucial role in enabling the JVM to execute the method without creating an object of the class.
Why Is main() Static?
- No Object Creation Needed: Since main() is a static method, the JVM can call it directly without needing to instantiate the class.
- Saves Memory & Execution Time: Avoiding object creation reduces memory usage and speeds up execution.
- Prevents Unnecessary Overhead: If main() were non-static, every execution would require an object, which is inefficient.
What Happens If main() Is Not Static?
Let’s see an example where the main() method in Java program is declared without static:
class Demo {
void main(String[] args) {
System.out.println("Will this work?");
}
}
Error Output:
Error: Main method is not static in class Demo, please define the main method as:
public static void main(String[] args)
Explanation:
- Since main() is an instance method here, the JVM cannot call it without an object.
- This violates Java’s execution model, causing an error.
- To fix this, we must declare main() as static.
Want to expand your knowledge of Java programming? Check out this amazing course and fast-track your journey to becoming a Java developer.
void Return Type Of Main Method In Java
In Java, the void keyword indicates that a method does not return any value. Since the main() method serves as the program’s entry point rather than producing a result, it is declared with void keyword for its return type.
Why Is main() Void?
- No Value Needs to Be Returned: The main() method is executed by the JVM to start the program, but it doesn’t return any result to the JVM.
- Follows Java's Convention: Standard Java applications follow this structure, ensuring consistency across different programs.
- Prevents Unexpected Behavior: If main() returned a value, there would be ambiguity about what the JVM should do with it.
What Happens If main() Has a Return Type?
Let’s modify the main() method in Java to return an int data type value and see what happens:
class Demo {
public static int main(String[] args) {
System.out.println("Can main() return something?");
return 0;
}
}
Error Output:
Error: Main method must return void in class Demo, please define the main method as:
public static void main(String[] args)
Explanation:
- The JVM expects main() to have a void return type.
- Returning a value disrupts the standard method signature, causing an error.
- To fix this, main() must be declared as void.
The main Identifier – Main Method In Java
In Java, main is not a keyword but an identifier—a name assigned to the method that serves as the entry point for program execution. The Java Virtual Machine (JVM) specifically looks for a method named main with the correct signature to start execution.
Why Must It Be Named main?
- JVM Recognition: The JVM is programmed to look for public static void main(String[] args). Any other name will cause an error.
- Standard Convention: Using main ensures uniformity across Java applications.
- Not a Reserved Keyword: Since main is just an identifier, it can technically be used as a variable or method name elsewhere in the program.
What Happens If We Change the Name?
Let’s modify the name of the main() method in Java to start and see how it impacts execution:
class Demo {
public static void start(String[] args) {
System.out.println("Will this work?");
}
}
Error Output:
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
Explanation:
- The JVM does not recognize start() as the entry point.
- It strictly looks for main() with the exact method signature.
- To fix this, we must name the method main.
String[] args In Main Method In Java
In Java, String[] args is a parameter passed to the main() method that allows users to provide command-line arguments when running a program. These arguments are received as an array of String values, which can be accessed inside the program.
Why Does main() Method In Java Have String[] args?
- User Input at Runtime: Allows users to pass values when executing the program without modifying the code.
- Enhances Flexibility: Programs can behave differently based on the provided arguments.
- Handles External Data: Useful in real-world applications, such as configuring settings, specifying file paths, or passing inputs to scripts.
How Command-Line Arguments Work
When running a Java program from the terminal, we can pass arguments after the class name. For example, consider this Java program:
class Demo {
public static void main(String[] args) {
System.out.println("First argument: " + args[0]);
System.out.println("Second argument: " + args[1]);
}
}
If we compile and run it using:
javac Demo.java
java Demo Hello World
Output:
First argument: Hello
Second argument: World
Breaking It Down:
- args[0] stores "Hello", and args[1] stores "World".
- The values are passed when executing the program.
- If no arguments are provided, trying to access args[0] or args[1] will cause an ArrayIndexOutOfBoundsException.
What If We Don’t Use String[] args?
If we modify the main() method in Java to remove String[] args here is what happens:
class Demo {
public static void main() {
System.out.println("Will this work?");
}
}
Error Output:
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
Explanation:
- The JVM requires the method signature to include String[] args, even if we don’t use it.
- If no arguments are needed, we can simply ignore the args array inside the method.
Here is your chance to top the leaderboard while polishing your coding skills: Participate in the 100-Day Coding Sprint at Unstop.
The Role Of Java Virtual Machine (JVM)
The Java Virtual Machine (JVM) is responsible for executing Java programs, and it plays a crucial role in identifying and invoking the main() method. Without the JVM, Java code is just text—it's the JVM that translates it into machine-executable instructions.
How Does The JVM Identify main()?
When a Java program is executed:
- Class Loading: The JVM loads the class that contains the main() method.
- Method Lookup: It searches for public static void main(String[] args).
- Execution Start: Once found, execution begins inside the main() method in Java programs.
If the JVM does not find a properly defined main() method, it throws an error:
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
JVM’s Role In Java Execution
- Compilation & Interpretation: Java code is compiled into bytecode (.class files) and executed by the JVM.
- Memory Management: The JVM handles memory allocation, garbage collection, and stack/heap management.
- Platform Independence: The JVM ensures Java runs on any system with a compatible JVM, making Java a "write once, run anywhere" language.
JVM & The main() Method In Java– A Visual Flow
Running Java Programs Without The Main Method
The main() method is the entry point of a Java application, but are there ways to run Java programs without it? The short answer: Yes, but with limitations.
1. Using Static Blocks (Before Java 7)
Before Java 7, Java features allowed code execution inside a static block without needing the main() method.
class Demo {
static {
System.out.println("Executed without main!");
System.exit(0); // Exiting to prevent JVM error
}
}
Output:
Executed without main!
Why Does This Work?
- The JVM executes static blocks when the class is loaded.
- System.exit(0) prevents the JVM from searching for main().
Why Doesn't This Work Anymore?
Since Java 7, the JVM strictly requires the main() method to exist, and missing it results in an error:
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
2. Using JavaFX Or Servlets
JavaFX applications and web-based servlets don’t always require the main() method to be there in Java programs. This is because:
- JavaFX applications use the Application class’s start() method instead.
- Servlets are executed by a web server, not the JVM’s main() method.
3. Using JNI (Java Native Interface) Or Custom JVM Launchers
Advanced developers can manipulate Java execution using JNI or custom class loaders, but these are not standard approaches for beginners.
This shows that the main() method is mandatory in standard Java applications. Workarounds like static blocks were possible before Java 7 but are now restricted. Also, there are some specialized Java applications (JavaFX, servlets) that don’t require the main() method in Java programs for successful execution.
Variations In Declaration Of Main Method In Java
While public static void main(String[] args) is the standard method signature, Java allows some variations in its declaration without breaking execution.
1. Changing Parameter Name
The parameter name args can be changed to anything as long as the type remains String[].
class Demo {
public static void main(String[] input) {
System.out.println("This still works!");
}
}
Output:
This still works!
✅ Why does this work? The JVM looks for String[] as the parameter type, not the variable name.
2. Using varargs (String... args) Instead Of String[] args
Java supports varargs (...), allowing main() to be written as:
class Demo {
public static void main(String... args) {
System.out.println("Varargs main method!");
}
}
Output:
Varargs main method!
✅ Why does this work? String... args is equivalent to String[] args in Java.
3. Reordering Access Specifiers
The order of public and static does not matter.
class Demo {
static public void main(String[] args) {
System.out.println("Access specifier order changed!");
}
}
Output:
Access specifier order changed!
✅ Why does this work? Java doesn’t enforce a strict order for public and static, as long as both are present.
4. Using final, synchronized, or strictfp Modifiers
Java allows some additional modifiers with main(), though they are rarely used.
class Demo {
public static final void main(String[] args) {
System.out.println("Final main method!");
}
}
Output:
Final main method!
✅ Why does this work? The JVM ignores final, synchronized, and strictfp when looking for main().
Overloading The Main Method In Java
Java allows method overloading, meaning multiple methods can share the same name but differ in parameters. This applies to the main() method in Java as well, but with a catch—the JVM will always call the standard main() method (public static void main(String[] args)) to start execution.
Overloaded main() Methods
class Demo {
// Standard main() method called by JVM
public static void main(String[] args) {
System.out.println("Standard main() called");
main(10); // Calling overloaded method
}
// Overloaded main() with an int parameter
public static void main(int num) {
System.out.println("Overloaded main() called with num: " + num);
}
}
Output:
Standard main() called
Overloaded main() called with num: 10
✅ Why does this work?
- The JVM starts execution with the standard main() method.
- The overloaded main(int num) method is explicitly called inside the standard main().
Can The JVM Call An Overloaded main() Directly?
No. The JVM only looks for public static void main(String[] args). Any overloaded main() will be ignored unless it is explicitly called within the standard main() method.
Why Overload main() Method In Java Programs?
- Organizing Code – We can use overloaded main() methods to separate logic.
- Handling Different Inputs – Overloaded methods can process different argument types.
- Interview Trick Questions – Knowing that the JVM only calls public static void main(String[] args) helps avoid confusion.
Need more guidance on how to become a Java developer? You can now select an expert to be your mentor here.
Conclusion
The main() method in Java is the entry point of any standalone Java application, serving as the first method executed by the JVM. It follows a specific signature—public static void main(String[] args)—to ensure consistency across Java programs.
Each component of the main() method in Java has a role:
- public ensures accessibility.
- static allows execution without an object.
- void means no return value.
- main is the recognized identifier.
- String[] args handles command-line arguments.
The JVM strictly requires the main() method, and failing to define it correctly results in an error. However, some variations in declaration are allowed, such as changing parameter names, using String... args, or modifying access specifier order.
It is also possible to overload the main() method in Java, but only the standard method is called by the JVM. Understanding the main() method is crucial for writing Java applications, as it dictates how programs begin execution.
Frequently Asked Questions
Q1. Can we have multiple main() methods in a Java program?
Yes, you can use multiple main() methods in Java programs through method overloading. However, the JVM only calls the standard public static void main(String[] args) automatically. The other main() methods need to be explicitly called within the program.
Q2. What happens if we remove static from main()?
Removing static keywords from the main() method in Java causes a runtime error because the JVM would need to create an object before calling main(). But since no constructor is provided for automatic instantiation, it will lead to error in execution.
Q3. Is main() mandatory in Java?
Yes, the main() method is mandatory in all standalone Java applications. However, Java frameworks like JavaFX, servlets, and Spring Boot can run without a main() method, relying on different entry points.
Q4. Can we write main() inside an interface?
No, you cannot write the main() method inside interfaces because they cannot have static methods before Java 8. Even in Java 8+, a static method inside an interface is not considered a valid entry point for program execution.
Q5. Why is String[] args used instead of String args[]?
Both notations– String[] args and String args[]– of the main() method in Java are valid. But String[] args is preferred as it clearly signifies an array of Strings.
Do check the following out for more interesting Java topic reads:
- Operators In Java | 9 Types, Precedence & More (+ Code Examples)
- One Dimensional Array In Java | Operations & More (+Code Examples)
- How To Install Java For Windows, MacOS, And Linux? With Examples
- Method Overriding In Java | Rules, Use-Cases & More (+Examples)
- Advantages And Disadvantages of Java Programming Language
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Esuru Pooja.C 2 hours ago
Ankita Das 2 days ago
Uma Maheshwari 4 days ago
Mihir Kumar Ranjan 1 week ago