Lew said:
You might find /Thinking in Java/ by Bruce Eckel very helpful on these
kinds of problems. It does a right fine job of helping one understand the
fundamentals of the Java language. Earlier versions of the book are
available for free on line.
There is not exactly a concept of "main class" in Java. In JAR files there
is a so-called "manifest" that tells a "java -jar" command what class to
run, but nothing prevents running the main() method of a different class
in that same JAR using other syntax.
There is a concept of a main() method in a class, and any public class
that has a properly defined main() method is a "main class".
If you are running a Java application from someone else, their
documentation should tell you what class to run. If it is your
application, then you get to choose any class for which you wrote a main()
method (obeying the rules of that method).
On the other hand, a couple of IDEs have a concept of a "main class"
(Eclipse is one of them), and that concept basically refers to which class's
main method the IDE should run when you tell it to run the application. Note
that every "run configuration" (that's the term Eclipse uses) has exactly
one main class, but a given project may have multiple main classes (which
implies multiple "run configurations" can be associated with a single
project).
So as Lew said, if it's your application, then you, as the designer,
just choose a class to be your main class. If you're inheriting code from
someone else, then you just gotta search for static void methods called
"main" which take an array of String as an argument (different IDEs have
different ways of searching through the code base). If you gotta do this
programmatically, then you iterate through all the candidate classes, using
reflection to check for the existence of such a method.
- Oliver