my dog learned polymorphism
The moose likes Java in General and the fly likes Where to use of the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Where to use of the "main" method" Watch "Where to use of the "main" method" New topic

Where to use of the "main" method

william christensen

Joined: Oct 14, 2005
Posts: 7

Does there need to be a "main (...)" method declared in at least one class that is in a package? If so, does the class holding the "main(...)" method need to be in a class whose name is the same as the package name? Can you enlighten me on the proper use of naming and lauching packages.

Many thanks
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

An application needs to have a main() for Java to be able to launch it. If main() is in a class that's in a package other than the default, it needs to be in a public class. The signature of main() must be

public static void main(String[] args)

That's it. There are no other rules.

It is never the case that a class name needs to be the same as a package name, for any reason; in fact, it's always a bad idea to name a class this way.

[Jess in Action][AskingGoodQuestions]
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
package files need not have a main at all

but if you want to use the "java" command on a class, it should have a main method that is in a class and the file containing it must have the same name as this class. it need not be public, but in that case, no other class in the file should be public. don't get confused, make a start somewhere and start exploring, a good place to ask such questions is the SCJP forum, you'll get replies with a lot of insights from beginners and experts

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
I agree. Here's the link:
subject: Where to use of the "main" method
It's not a secret anymore!