Java: Multiple class declarations in one file

If you already know why the following code fail to compile, then you don’t need to continue reading!
// File: MyClass.java
public class MyClass {
// field, constructor, and
// method declarations
}
public class MyClass2 {
// field, constructor, and
// method declarations
}

However, if you are surprised then continue reading:

Above code fails to compile because there are two public classes declared in a single file.
If you need to define multiple classes in a single file you need to declare only one Public top level class.
For example, below code will successfully compile:

// File: MyClass.java
public class MyClass {
// field, constructor, and
// method declarations
}
class MyClass2 {
// field, constructor, and
// method declarations
}

The purpose of supporting multiple top level classes in one file is to be compatible with version 1.0 before nested classes where introduced.
Instead of declaring multiple top level classes in one file you can have one public top level class and then have as many inner classes as you like. This way you can combine the main public class with its support functionality.
You can read more about inner classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html