I know that the specification dictates so, but that is not my concern, but to find out why.
public class Clase1{
}
public class Clase2{
}
Or it could also be:
public class Clase1{
}
public enum enum1{
}
Or it could also be:
public class Clase1{
}
public interface T{
}
The compiler says that each class is in a file named the same way.
To answer this type of question you must go to the Java Specification
pagina section 7.6 top level type declaration
as we can see in the example code:The specification defines that there is no restriction for this case, wait if we say that it is possible to add more than one public class in a .java file, why does the test fail? if you just proved it in your question, the simple answer is
porque cuando se creó el compilador de java se definió esta restricción
. Now to understand the reason beyond aporque si
, we must know that the compiler requires that for allclase A
there must be at least one file with the nameA
and extension.java o .jav
in the specified package, this is because when the process ofcompiling-linking-loading
making accessible the compiled code to the other classes this makes it easy for theCompilador de Código Java
or an implementation of a class toCompilador de Código Java por ejemplo el de OpenJDK
find the class within the same package.Imagine the following context:
In this case we have one
clase A
that refers to oneclase B
in the same file, when the process is carried outcompiling-linking-loading
when compiling the classA
it refers to the classB
that is not yet compiled, as both classes are in the same file and more than one class is allowed per file, the compiler has to check all the classes inside the packagees.java
to establish that the oneclase B
to be compiled is indeed the one inside the same fileclase A
and not inside another file.java
that is inside the same packagees.java
to rule out a duplicate class , because remember that for classes in the same package there is noimport
specifying the exact path of the class to the compilerB
in this case.In conclusion: The restriction is to optimize compilation speed and all other compilers inherited the restriction to maintain the Oracle compiler standard and that the code they compile is 100% compatible between all compilers, even though the standard does allows it.
And for nested classes, which in the end are one or more public classes within another:
Well, it is only visual, the compiler separates the class into different compiled classes, each with its respective name, if you compile the code that I show, it
javac YoSoyTuPadre.java
generates 3 classes ,YoSoyTuPadre.class, YoSoyTuPadre$Hijo1.class y YoSoyTuPadre$Hijo2.class
placing the reference in the name to know that it was compiled from a nested class.Java was built from the ground up to be completely object oriented. Everything in Java is an object (with some exceptions), and everything in Java resides in a class (remember that a class is a template from which various objects can be created).
The source code is saved in files with the same name as the class they contain and with the extension “.java” . A class declared public must follow this convention. If we have a class called Hello, its source code must be saved in the file “Hello.java”.
The compiler generates a class file (with extension “.class”) for each of the classes defined in the source file.
It's all in the official Java language specification