Valid variants of main() in JavaThe main method is the starting point for executing Java code. A runtime exception will be thrown if the JVM cannot locate a main method during runtime. In other words, if no main method is present in the Java code, the JVM will report an error at runtime.
Although the syntax of Java's main() method is generally strict, some minor modifications are allowed. These modifications do not result in a runtime exception. As a result, there are various valid variants of the main() method in Java. Understanding these variants is essential as they allow developers to modify the standard syntax slightly to suit their specific needs. Default prototypeThe following is the standard and widely accepted way to write Java's main() method. Filename: Computer.java Output: Welcome Order of ModifiersThe static and public modifiers in the main() method declaration can be interchanged without affecting the program's behavior. Filename: Computer.java Output: Welcome Variants of String array argumentsIn Java, it is possible to place the square brackets at different positions when declaring the String parameter in the main() method. Filename: Computer.java Output: Welcome Filename: Computer.java Output: Welcome Filename: Computer.java Output: Welcome Args or anythingIn the main() method in Java, you can use any valid Java identifier instead of args, such as your name, company name, or any other word or phrase that follows the rules for Java identifiers. Filename: Computer.java Output: Welcome Var-args instead of String arrayIn Java, the main() method can be declared with a variable-length argument parameter (varargs) instead of a String array. It provides more flexibility in handling command-line arguments. In Java, if the String parameter in the main() method is a one-dimensional array, you can use a varargs parameter (denoted by three dots ... instead of square brackets []) as a replacement. Filename: MainMethodDemo.java Output: Number of arguments: 0 The final Modifier String argumentIn Java, it is possible to declare the String[] args parameter in the main() method as final, ensuring that the parameter's value cannot be changed. Filename: MainMethodDemo.java Output: Arguments: Final main methodIn Java, the final keyword can be applied to the main() method to prevent the method from being overridden in any subclass. It is possible to declare the main() method with the final keyword, which does not affect the execution of the program or cause any errors. Filename: DemoExample.java Output: It is a final main method Synchronized Keyword to Static Main MethodIt is possible to declare the main() method synchronized in Java. Filename: ComputerDemo.java Output: Synchronized Main Method strictfp keyword to the static main methodIn Java, the strictfp keyword ensures consistent floating-point arithmetic across different platforms. Filename: DemoStrictfp.java Output: x + y = 1.0 Overloading the Main methodWe can overload the main() method in Java like any other method. Overloading means creating multiple methods with the same name but with different parameters. We can have any number of overloaded main() methods, but the one with the signature public static void main(String[] args) will be the entry point of the program. Filename: MainOverloading.java Output: It is the main() method with String[] argument Inheritance of the Main methodIn Java, you can inherit the main() method from a superclass to a subclass as long as the method signature remains the same. If the subclass does not define its own main() method, it will inherit the main() method of its superclass. If the subclass does define its own main() method, it will override the main() method of its superclass. JVM Executes the main() without any errors. Filename: Animal.java Output: It is the main method of the Animal class Method Hiding of main(), but not OverridingIn Java, it's possible to "hide" the main method of a parent class in a child class using method hiding, but it's impossible to override it since it's a static method. Here's an example of a method hiding of main(): Filename: Animal.java Output: It is the main method of the Animal class Next TopicWhat is a Reference Variable in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India