Solved

Why main method required in java

Posted on 2003-11-09
2
268 Views
Last Modified: 2010-04-17
Why the signature of main method be always public static void main(String args[])?
What actually happens when we write >> java <classname> <argument1> ?
0
Comment
Question by:amol_ingle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9712763
Its public static void main for the following reasons.
public since we need to access it outside out class
static since we dont create an instance of the class for executing main
void is the return type (since we are calling from outside return value serves almost no purpose.
string args[] is for storing the arguments as an array

when u call the java class with arguments the arguments are stored in the array and you can access them later in your code.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 250 total points
ID: 9713241
>> What actually happens when we write >> java <classname> <argument1> ?

1) The Java Virtual Machine is started ("java") and it attempts to load the specified class ("<classname>").
2) When the class is successfully loaded, the JVM looks for the main() method entry-point and passes the remaining command line parameters ("<argument1> ...") to this method as an array of Strings.

That's why if you make a typo on the command line, step 1) responds with "NoClassDefFoundError".
If main() is not defined correctly, then step 2) responds with "NoSuchMethodError: main"

Just for information: The main method signature should *always* be the same.  The Java Virtual Machine specifically looks for this method, so if you were to write:

public static void main()
{
   .
   .
}

It wouldn't run.

Also, because Java is fairly relaxed about array definitions, you can also use:

public static void main(String[] args)

(This is the preferred way to define an array - ie. with the [] following the class/type rather than the variable - but both will work OK)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question