Put all source files into a directory, one class per file
When you start a new project, create a new directory for the source files. The directory name should be lowercase letters, with no blanks or other punctuation. The directory is used as the package.
Each class should be defined in its own .java file
Put each of your classes in its own file. It's easy to do. Here are a couple of minor rules.
- Make sure all classes (.java files) are in the same directory (folder).
- You need separate import statements for each class.
- You may have to compile a class that is referenced before the class that uses it. Whether you have to do this or not depends on which devopment system you're using.
- When you run the program, you may have to have the class that
main(. . .)be visible in your development system (depends on the development system.
There are some exceptions (inner classes), but this is the general rule that applies to almost all Java programming.
It's possible to put more than one class in a file and have everything work. I did this for a long time for small programs, but have given it up because it doesn't scale up as you create larger programs and use more better development tools. A common development tool, Ant, works best when each class is in its own source file.
Multiple classes of larger programs are usually grouped together into packages. See Packages - Defining for how and why to use packages.