First,its not neccessary that you have to specify your classname as public class abc in "abc.java". You can save the file with a different name.For eg,if you have made a class A and save it as B.java then while compiling it give javac B.java and an A.class file will be created. While execution,give java A.The .class file will be taken and the code will work. public class is used only when you have the file in a different package. regards, shalina
I just want to rearrange Shalina's thought. Shalina is right. You don't have to give the exact same file name as your java class. You don't have to indicate the public modifier in your class too. In my experience, these rules are applied with some constraint. You must save the same file name, i.e. java source file, as your Class name given in the code if you use "public" identifier. If you don't use the "public" identifier, then you can use any name with .java extension to your source. Issue javac command to the filename, and class file with the same name given inside the code will be created. Please correct me if i am wrong.
Joined: Nov 15, 2000
"You must save the same file name, i.e. java source file, as your Class name given in the code if you use "public" identifier. " why ??please tell me asheet
Asheet.... This is what I THINK is the reason (new to java so I am not positive)..... When you have a class which contains the main which you will start executing...it has to be public to be executed from the command line. And since you are starting up java, it needs to know which main to start out of all that may be out there. But by saving the file as the same class, it will use the main in that file. Dan
This is just a mechanism to help out the operating system in finding the class and to keep you from shooting yourself in the foot. If you COULD put a public class in a different filename imagine: You have public class MyClass in file A.java and you compile it. Since the class is public it then gets used by stuff in other directories. But there is NO MyClass.java hanging around. A month later you come along doing something completely different. You create a MyClass.java and save it. You will NOT be getting any message from the operating system saying that a file with "this name already exists - Do you really want to overwrite it?" Now you compile over the other MyClass.class and all the other code in other packages is broken. Of course this could happen to you with your non-public classes if you are not carefull, which is why many places enforce the "one class per file rule" so the operating system will help track class names for you.
From the JVM Specification 7.6 Top Level Type Declarations
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.
"JavaRanch, where the deer and the Certified play" - David O'Meara