wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Interesting Question!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interesting Question!!!" Watch "Interesting Question!!!" New topic
Author

Interesting Question!!!

asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
hi there,
could you please tell me why i have to save a file with public class "abc" in "abc.java",as when i dont make this class public then i can save this file with a different name.
thanks
asheet
shalina gokarna
Greenhorn

Joined: Jul 01, 2001
Posts: 10
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
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
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.
asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
"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
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Good explanation Cindy.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Who here remembers Toad the Wet Sprocket?

Someone writing the Java spec must be a fan.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

Toad the Wet Sprocket?

Is he related to that silly paperclip that keeps popping up without permission?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Close.

Have you seen these? http://www.microsoft.com/Office/clippy/downloads.asp
Three little flash animations that I can't believe Microsoft made. But I laughed out loud when I watched them, and tittered away to myself for the rest of the afternoon.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
LOL!! Just watched those clips!!
Well, at least they have a sense of humor .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting Question!!!