Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to use of the "main" method

 
william christensen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

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
William
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic