Valid Variants of main() in Java
In Java, the main() method is the entry point for a Java application. It has a specific signature that must be followed, although there are valid variations in the method's modifiers, return type, and parameter list. Here are some valid variants of the main() method in Java:
1.The standard variant
public static void main(String[] args) { // Code goes here }
This is the most commonly used variant of the main() method. It is declared as public (accessible from anywhere), static (can be called without creating an instance of the class), and has a return type of void, indicating that it does not return any value. It takes an array of strings args as a parameter, which represents the command-line arguments passed to the program.
Example 1
FileName: AddNum.java
public class AddNum { public static void main(String[] args) { // Declare and initialize variables int num1 = 5; // First number int num2 = 10; // Second number int sum = num1 + num2; // Sum of num1 and num2 // Display the sum System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); } }
Output
The sum of 5 and 10 is: 15
2. Using the varargs syntax for command-line arguments:
public static void main(String... args) { // Code goes here }
This variant uses the varargs syntax (String... args) for the parameter instead of explicitly specifying an array. It allows you to pass multiple command-line arguments separated by commas or even pass an array directly.
Example 2
In this program, the main() method uses the varargs syntax for the args parameter, indicated by the String... declaration. This allows you to pass multiple command-line arguments when executing the program.
FileName: VarargsProgram.java
public class VarargsProgram { public static void main(String... args) { System.out.println("Command-line arguments:"); // Loop through each command-line argument and print it for (String arg : args) { System.out.println(arg); } } }
Output
//Run with this command java VarargsProgram arg1 arg2 arg3 Command-line arguments: arg1 arg2 arg3
3. Adding additional modifiers (e.g., final, synchronized, strictfp):
public static synchronized void main(String[] args) { // Code goes here }
In addition to the standard modifiers public and static, you can add other modifiers to the main() method. For example, synchronized ensures that only one thread can execute the method at a time, final prevents overriding the method in subclasses, and strictfp enforces strict floating-point precision.
Example 3
The code will first print "Modifiers Program" as the output of the line System.out.println("Modifiers Program");. This indicates the start of the program's execution. After that, the code will print "Command-line arguments:" to indicate that it will display the passed command-line arguments.
FileName: ModifiersProgram.java
public class ModifiersProgram { public static final synchronized strictfp void main(String[] args) { System.out.println("Modifiers Program"); System.out.println("Command-line arguments:"); // Loop through each command-line argument and print it for (String arg : args) { System.out.println(arg); } } }
Output
Modifiers Program Command-line arguments:
4. Returning an integer exit code:
public static int main(String[] args) { // Code goes here return 0; // Exit code }
The main() method can return an integer value, which conventionally represents the program's exit status. By convention, a return value of 0 indicates successful execution, while a non-zero value indicates an error or abnormal termination. Returning an exit code is optional.
Example 4
In the given program, the main() method calls the performCalculation() method to perform some calculation or logic. The result of the calculation, which is the sum of num1 and num2, is stored in the result variable.
FileName: ExitCodeProgram.java
public class ExitCodeProgram { public static void main(String[] args) { int result = performCalculation(); System.exit(result); } private static int performCalculation() { // Perform some calculation or logic here int num1 = 5; int num2 = 10; int sum = num1 + num2; // Return the exit code return sum; } }
Output
The code itself does not display any output. However, the exit code (15) can be captured and observed externally, depending on how the program is executed.
5.Using the throws clause to specify checked exceptions that may be thrown:
public static void main(String[] args) throws IOException { // Code goes here }
If your main() method performs operations that may throw checked exceptions, you can use the throws clause to specify those exceptions. This indicates that the exceptions may be propagated up to the caller or caught within the main() method itself.
Example 5
FileName: ThrowsExample.java
import java.io.FileNotFoundException; import java.io.FileReader; public class ThrowsExample { public static void main(String[] args) throws FileNotFoundException { try { readFile("example.txt"); } catch (FileNotFoundException e) { // Catching the FileNotFoundException and printing an error message System.out.println("File not found: " + e.getMessage()); } } private static void readFile(String filename) throws FileNotFoundException { // Attempting to create a FileReader for the specified filename FileReader fileReader = new FileReader(filename); // Additional logic to read the file } }
Output
File not found: example.txt (No such file or directory)