Hello there, I have just picked up learning Java. . . and I just wanted to know what was the thinking behind the developers to make only one public class per file and also give the file name as per the public class.
"Olly", Hi, welcome to the ranch. You may not be aware of the ranch's Naming Policy, so please read it carefully and change your name accordingly. (you need to set both first and last names). Thank you.
*I* think the point is that each compilation unit has a single public interface represented by that public class. Now, this allows the source for a given class to be found by a compiler or a debugger e.g. when you compile a single class the compiler will search for source files for external references that might not have been compiled, or have later modified many times than the coresponding class files, and compiles them too. Thus when a debugger halts at a statement it can it can provide the actual point of discrepancy.
When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
* The type is referred to by code in other compilation units of the package in which the type is declared. * The type is declared public (and therefore is potentially accessible from code in other packages).
This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.
Thank you!, Anubhav & Joe for you replies. Its a bit difficult for me to grasp your answers as they have lots of technical jargon. However: 1. The Java compiler in itself is a very very complex piece of software, so...putting a restricting on the Java developer to name his file just for the sake of get the compiler to work a bit easier isn't fitting into my understanding. 2. Even so what about the non public classes that a programmer can write into his source files, I mean in addition to the single public class there may be lots on other classes too!. . . Thanks!