- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Valid variants of main() in Java
In Java, the main() method is the entry point from where the JVM begins program execution. If you've written a Java program, you're likely familiar with the traditional main() signature: public static void main(String[] args). However, did you know that there are several valid variants of the main() method in Java? This article delves into the versatility of main() in Java, showcasing its multiple valid formats and explaining their intricacies.
The Canonical Main() Method
Before delving into its
public static void main(String[] args)
In this format, public denotes that the method can be accessed from anywhere; static means the method belongs to the class itself and not to any instance of the class; void indicates that the method doesn't return a value; and String[] args is the parameter, an array of String objects, which stores Java command-line arguments.
Valid Variants of Main() in Java
Although the above main() method signature is the standard, Java supports several other valid variants, thanks to its flexibility. Let's examine them −
Changing the Order of public and static
The keywords public and static can be interchanged without affecting the execution of the program −
static public void main(String[] args)
Using final, synchronized, and strictfp Modifiers
The main() method can be declared with final, synchronized, and strictfp modifiers without any issue −
final public static void main(String[] args) synchronized public static void main(String[] args) strictfp public static void main(String[] args)
Modifying the String Array's Syntax
The String array (String[]) syntax can be modified in the following ways:
public static void main(String args[]) public static void main(String... args)
Changing the Name of the Argument Array
The name of the argument array (args) can be replaced with any valid identifier −
public static void main(String[] myArray)
It's essential to note that while these variants provide flexibility, they may not conform to standard conventions. In professional programming environments, the canonical main() method signature is typically expected.
Invalid Main() Method Signatures
There are certain changes to the main() method that Java does not accept, rendering the program unrunnable. Here are some examples
Removing static from the main() method
Changing the return type from void to anything else
Changing the parameter of main() to anything other than String array
These changes will compile, but the JVM will not recognize the altered main() as the program's entry point.
Conclusion
In Java programming, understanding the workings of the main() method is crucial, as it forms the program's starting point. While the canonical public static void main(String[] args) is the most recognized and frequently used, Java's flexibility supports various valid main() method variants.
However, keep in mind that sticking to the standard format is generally best practice, as it ensures consistency and readability across different programs and projects. As you continue your journey into Java programming, knowledge of these nuances will empower you to write and understand Java code more effectively, demonstrating your mastery over this versatile language.