Meaning of public static void main(String args[])

Today we will discuss a very basic but important topic.
It is a important question in Java interviews for beginner level.

Why do we declare as
 public static void main(String args[])

I have break the meaning of this statement in five parts for better understanding.

public

It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.

static

We want the method to be available when the class is loaded. When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.

void

Java is platform independent language and if it returns some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multi-threaded system. So We want the main method not to return any value.

main

It's just the name of the method. This name is fixed and as it's called by the JVM as entry point for an application.

String args[]

These are the Command Line arguments we pass to the Java application when complied with javac.

Hope you guys like it. Please comment for doubts.
Happy Learning !!!
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment