The Main Java class

Hey guys, I have a few more Java Questions, I kind of get it but I don’t.

  1. What determines your class is the MAIN one?

If you have a folder with:

How do you let javac know that main.class is the MAIN one you want to use, and all the other ones are just called inside of it?

Do you just say javac, and it will compile all the imported classes?

I thought I remember a function called Main(), but can’t that be used in ANY class?

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)

//compile all classes in the current directory
javac *.java

//compile two classes

Docs for javac

Yes, you can put it on any class. Create a special “main” method on your chosen class.

    public static void main(String[] args) {

You could have a multiple main classes.
compile as normal (using javac).

To pick the one you want to run, specify it as a argument to ‘java’


java MyMainClass

Hey thanks for response–

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 {
    // this would be the construct?

In Java the constructor is the same string name


The constructor is a special method that has the same name as the class, may or may not accept arguments and has no return valve, not even void.

If you do not specify a constructor, Javac will insert the default constructor:

class name{ super(); }

So there should only be one main() method inside a group of classes?

Not really.

I can write ‘programs’ (classes that have a main and can be invoked with java.exe):

and have them all invoke the classes

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.

well that was easy thanks so much :smiley:

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!