Explain the following line used under Java Program - public static void main (String args[ ])

0 votes
asked Jun 30, 2016 in Java by Nani
, , 154 views Last updated : Dec 13, 2017

2 Answers

+1 vote

Java is a pure Object Oriented Language. Anything you write in a Java Program should reside in a Class. Assume you have write a simple java program. So you write a Class named that contains the methods which perform your logic. 

Now to run your logic you have to create an object to your class. So to create an object you should have atleast one method that have access with out creating an object. Hence the main method was declared as "static". Now the Java Virtual Machine(JVM) can call the main method as ClassName.main()

public is the access specifier which tells that the main method can be accessed from outside the class.

void is the return type of the main method.

String[] args  is used to pass any arguments. Java only accepts the array of Strings as arguments.

In your question, the arguments are written as String args[]. Though it doesn't give any error it is always a good programming habit to declare an array as "TypeName[] variabeName"  for example... "String[] args".

answered Sep 21, 2016 by Naveen Sahukari
0 votes

The following shows the explanation individually - 

  1. public - it is the access specifier 
  2. static -  it allows main() to be called without instantiating a particular instance of a class 
  3. void - it affirns the compiler that no value is returned by main()
  4. main() -  this method is called at the beginning of a Java program 
  5. String args[ ] -  args parameter is an instance array of class String 
answered Jun 30, 2016 by Surya

Related questions