Explain-each-keyword-of-public-static-void-main-string-args

Explain-each-keyword-of-public-static-void-main-string-args

Explain each keyword of public static void main(String args[])

Answer: 
public: public is a Java keyword which declares a member’s access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final.

static: static members belong to the class instead of a specific instance.

It means that only one instance of a static field exists even if you create a billion instances of the class or you don’t create any. It will be shared by all instances.

Since static methods also do not belong to a specific instance, they can’t refer to instance members (how would you know which instance Test class you want to refer to?). static members can only refer to static members. Instance members can access static members.

Note: static members can access instance members through an object reference.

void:  The return type and data type of the value returned by the method, or void if the method does not return a value.
Note: All methods in Java must be one return type. void tell’s to the JVM that specific method will not return anything.

main(String args[]) : main is just name of the method and args contains the supplied command-line arguments as an array of String objects.

For example: if you run your program as java MainMethodTest main test then args will contain [“main”, “test”].

Note: args doesn’t necessarily have to be named args (you can name it whatever you want) – though it’s best to follow convention. You also might see String… args from time to time, which is equivalent.

Leave a Reply

Your email address will not be published. Required fields are marked *