Explain Java Main Method public static void main (String[] args)
In the last post, we have seen how much Java is required to learn Selenium and today in this post we will see detail explanation on Java main() method i.e., public static void main(String[] args)
When you start learning Java, the first method you encounter is public static void main(String [] args). The starting point of any Java Program is the main() method. It is one of the important methods of Java. Technically, the main method is the starting point where the Java program starts its execution. JVM always look for this method signature to start running an application.
Syntax of the main method is as follows
class SoftwareTestingMaterial { public static void main(String[] args) { System.out.println("Learning from SoftwareTestingMaterial"); } }
Note: public static void main(string[] args) can also be written as public static void main(String args[]). Don’t get confused.
Each word has its purpose. Let’s break the method signature and see in detail.
public:
public is an access modifier. The scope of public access modifier is everywhere. It has no restrictions. Data members, methods and classes that declared public can be accessed from anywhere. If you make a main() method public then it is allowed to be executed by any program globally. If you make a main() method non-public then it is not allowed to be executed by any program.
static:
JVM can not create an object of class at run time to access the main method. By declaring the main() method as static, JVM can invoke it without instantiating the class. If we don’t declare the main method as static then JVM cannot call it to execute the program.
void:
Void means the Method will not return any value. In Java, every method provides the return type whereas Java main method doesn’t return any value. Java program starts with main method and terminates once the main method is finished executing. If we make main method to return a value, JVM cannot do anything with the returned value. So there is no need to return any value.
main:
main is the name of Java main method. It is the first thing that runs when you compile and run the Java program. The main method is searched by JVM as the starting point where the Java program starts its execution.
String[] args
String[] args is a parameter that accepts inputs. The name of the String array is ‘args‘ but it can be of anything based on users choice. The type of args is always String[] because Java main method accepts only a single argument of the type String array. We can replace the array name with anything then also it works fine. i.e. String[] raj or String[] stm
Related posts:
- Java Tutorial for Software Testers
- Java Interview Questions for Software Testers
- How To Explain Test Automation Framework In The Interview
- Explain WebDriver driver = new FirefoxDriver()Â
- Explain Selenium WebDriver Architecture