There are no limitations on how many classes can be used in a single Java programme. However, only one class should be declared using the public access specifier in any Java programme. Two public classes are not compatible with one Java programme.
As we are aware, a single Java source file (also known as a. java file) might contain one class or multiple classes. Therefore, if a.java file contains many classes, each class will be converted into a distinct class file.
Why do we have one class per file?
One class per file is something I really prefer. The reason is that Solution Explorer makes it easier for me to find the class files. I can locate the class without having to look at the class view. Additionally, it’s likely that the classes will expand as your application expands.
Can we have multiple classes in a single file?
We can not have multiple classes in a single file, you must make sure that only one class among those you define in a single Java file is public. A compile-time error will be produced if a single file has more than one public class. We can have multiple classes in File but only one class can be made public. If we try to make two classes in File public we get the following compilation error.
Why we cannot put two public classes in one Java file?
This is so that we can avoid confusing the JVM later on about which class should serve as its initial class, as there is only one public class that contains the public static void main(String args[]) method