The one, true main() method that appears in your program must have a certain signature to be recognized by java.exe as the proper main method to start your program with. That signature is:
public static void main( String args ){…}
Having the main() method is what makes a class the ‘main’ class, it is not necessary to include ‘main’ in the name of the class.
Conversely, javac.exe doesn’t care whether a class is a ‘main’ class or not. Javac will compile all classes it determines are necessary to compile the specified class.
If a class is not specified for compilation, but is included in a class that has been specified, and the source file of this class is older than the class file (iow: has not been changed since the last time it was compiled), that class will not be compiled. (javac is lazy and will only compile what it must compile to accomplish the task at hand)
examples
//compile all classes in the current directory
javac *.java
So there should only be one main() method inside a group of classes?
I’m going to have to look at constructors again… I got a huge book but haha, Im having a hard time thinking of sample applications to make without getting too detailed (the swing GUI editor generates too much code, I wont learn that way). I like swing, but I dont want to use it if I dont understand these basics yet.
There are a lot of things in Java it makes it overwhelming, I dont know how other people get started.
In Java the constructor is the same string name ie:
class Fancy {
Fancy(a)
{
// this would be the construct?
}
}
So there should only be one main() method inside a group of classes?
Not really.
Example:
I can write ‘programs’ (classes that have a main and can be invoked with java.exe):
A.java
B.java
C.java
and have them all invoke the classes
X.java
Y.java
Z.java
And have everything in the same package (or folder)
To confuse you even more, I can have program A instantiate B and C objects - simply having a main method does not disqualify a class from being instantiated in another class. The only main that will be run is the one in the class supplied to java.exe (unless, of course, you invoke main on one of the objects you create)
The only special thing about main is that (given: java YourClassName) java.exe will attempt to invoke YourClassName.main( args ), and complain if it doesn’t find it.
Yes that can sound confusing, LOL. I had to read it a few times. I think the easiest thing for me to do will keep it to one main() statement, in one file which will be the compiled one.
Ill have to checkout some code conventions to see what best practice is for organizing folders. Thanks for the descriptions!