top of page
Search

Understanding the MAIN method in JAVA

Writer's picture: Titash RoyTitash Roy

Introduction We are used to write the main method in JAVA, but have we ever thought of the way we write it and why? Why is the main method required in JAVA. What would happen if we write a functionality in JAVA that doesn't include the main method? In this post, we will mainly focus on understanding the use and properties of the main method.


What is the MAIN method? We can write a JAVA class without the main(), it wont through any error during compilation, but to execute a Java program we need to signal to the Java Virtual Machine where to start executing the program. The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program and throw an exception during run time - java.lang.NoSuchMethodError: main. The syntax of main method is always: public static void main(String[] args)


Can we use any other method as the Main()? Inside the JVM, the code is such that, it searches for a method, with the name of main as a starting point for the execution of the program. If we want to use any other name for the main method, we will have to customize the code inside JVM to search for a method with our given name for starting the program execution. NOTE: In a nutshell, it is possible to use our own method as the main method, but JVM customization is required.


Syntax of the Main() As we have seen above, the main() has the below syntax. public static void main(String[] args). Let's discuss these in details.

  • public - It is an access specifier. We should use a public keyword before the main() method so that JVM can identify the execution point of the program from anywhere in the project. If we use private, protected, and default before the main() method, it will not be visible to JVM.

  • static - JVM should be able to call the main() without object creation. Static methods are the methods which gets invoked without creating the objects, so we do not need any object to call the main() method. Moreover, main() should not be associated to any object.

  • void - In Java, every method has the return type. Void keyword acknowledges the compiler that main() method does not return any value. The main() is called by the JVM and as the JVM has nothing to do with the return value, we keep the main() as void.

  • main() - It is a default signature which is predefined in the JVM. It is called by JVM to execute a program line by line and end the execution after completion of this method.

  • String[] args - The main() method also accepts some data from the user. It accepts a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values.


Altering the syntax of the Main() Even though the syntax of the main() is strict, there are few modifications possible. Below are the few allowed modifications:

  • The order of the modifiers is not important. In other words, we can also write - static public void main(String[] args)

  • The argument that we pass in the main method, can be any valid variable.

  • The string array inside the argument can be declared in any acceptable form. It can be String[] args or String args[]. Also the String array can be written in var arg format(String... args)

  • We can also add some additional modifiers to the main method. As below - -->strictfp: strictfp keyword ensures that we will get the same result on every platform if we perform operations in the floating-point variable. -->synchronized: Can be added if the program is performed by a single thread. -->final: The main() can not be overridden in the subclasses, so we can use the final modifier as well.


More important facts on the main()

  • The main() can be overloaded, but the JVM always executes the one following the actual syntax. The other method needs to be called explicitly like normal method.

  • Inheritance concept also holds true for the main(). In case the child doesn't have a main method, the parent's main method is invoked.

  • If the child class also has the main(), with the same syntax, then the concept of method hiding is followed and not method overriding.

59 views0 comments

Recent Posts

See All

Comentarios


bottom of page