Java Interview Question – Explain public static void main(String[] args)

This is an important interview question in Java and mainly asked to Freshers. I will try to explain my best it here.

A Java program has a starting point of execution with a predefined signature of main method in a class. Method signature of main method is given below:-

This could be also written as:-

Let me break above method signature in to pieces so that you will understand it better.

public“: It is a Java keyword first so written in small case. It is an access specifiers or modifiers. Access modifiers are keywords in Java that set the accessibility of classes, methods, and other members. A public method can be accessed globally. Java Runtime Environment needs to call this main method outside of the class which is only possible if main method is made public. Declaring main method with any other access specifiers will not allow JVM to run it. If you do so, that main method will be considered as a normal java method and JVM will look for the main method with expected signature and throw error “Main method not found in class”.

static“: It is a Java keyword first so written in small case. Members declared as static in a Java class belong to class not its instance or object. When a Java class is loaded into memory, all its static members are also loaded in to memory unlike non-static members. You can access any static members without creating an object of class.

JVM can not create an object of class at run time to access main method. So only way is to declare main method as static so that JVM can load the class in to memory and can access static main method and start execution of program.

void“:- It is a Java keyword first so written in small case. When a method does not return any value, we need to specify void in method signature. A Java program starts with main method and ends as soon as execution of main method finishes. So if you make main method to return something, you can not use returned value.

main“:- It is method name which is fixed. JVM searches for the method with name “main”.

String[] args“:- Java “main” method accepts an argument of type String[]. “args” is just a name of arguments and you can change it to anything. But you can not change type of args. It must be String[]. We can pass args value from command line or run configuration. When we do not pass any argument, its size is zero.

Hope, I am able to explain in details. If you have any doubt, concerns or feedback, do let me know.

#ThanksForReading

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: