Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Why the name of a file that contains java code should be same as the name of the class that is contained init?

Thread view: 
corejavagroups - 20 Nov 2005 06:00 GMT
Hello Friends!
I am new to java area.
Why should we give the name of a file as same as the name of the class
that is contained in it?
Can you please explain it?
Please don't send me the links....i know thousands of links
available...but not in a proper way...that's why i am asking for a
straight answer......if you don't know please Ignore it
Once again i am telling i am new to java platform....
OK Thank you...
Bye
pit.grinja@gmx.de - 20 Nov 2005 08:24 GMT
Hi ...ehm... "corejavagroups".
> I am new to java area.
Your nick indicates this...
> Why should we give the name of a file as same as the name of the class
> that is contained in it?
How do you invoke a java programm that consist of only one class ? You
give the JRE the name of the class, and the JRE looks for a file called
"ClassName.class" and loads the class. If it is not there, you get an
error. Have you thought of the confusion that could arise when this is
not required? How would java find a class with a specific name if it
didn´t know in what file it is located? And if you were not talking
about *class files, but about source code: How would you look for the
source of the public class "HelloWorld" when you didn´t have any clue
about the name of the source code file?
> Can you please explain it?
> Please don't send me the links....i know thousands of links available...
> but not in a proper way...
And YOU know what is proper...
> that's why i am asking for a straight answer......if you don't know please Ignore it
> Once again i am telling i am new to java platform....
> OK Thank you...
You´re welcome. But now stop philosophising and write some code! There
are MANY MORE questions waiting for you...
Good luck
Piet
ameyas7@yahoo.com - 20 Nov 2005 10:47 GMT
hi

IT IS NOT MANDATORY (unless the class is public).
If you write a class

class SampleClass
{
....
}
you can name it MyClass.java because the class is not public.

Only for public classes it is MANDATORY to have the same name for the
java file.

as for the reasoning im not 100% sure but its better to have public
class A defined in A.java because your class is acessible to public
(i.e across your package) so if required they should locate it easily.

but when your class is not public then it is visible only within your
package so you know which file has which class.

that is how you can also write 3-4 classes in the same file provided
they are not (or at most 1 is) public.

when you run java you dont give it the filename the argument is the
ClassName (which contains main method)

HTH
cheers
amey
Roedy Green - 20 Nov 2005 14:34 GMT
>Only for public classes it is MANDATORY to have the same name for the
>java file.

You are teaching bad habits. It is not mandatory to brush your teeth
each morning either.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 20 Nov 2005 14:31 GMT
On 19 Nov 2005 22:00:03 -0800, "corejavagroups"
<sankar_battula@yahoo.co.in> wrote, quoted or indirectly quoted
someone who said :

>Why should we give the name of a file as same as the name of the class
>that is contained in it?

It makes like easy for the compiler to find the source code for a
class it needs.  When compiling class A you make reference to a method
of class B, Java has to find the corresponding class B file or if that
is missing, the corresponding source so that it knows how to generate
the code in class A. Having a consistent naming system makes that
faster.

It also makes it easy for programmers to find the right code given a
class name from JavaDoc or other source.

I am so glad Java enforces this rule.  Other languages create needless
chaos by not enforcing something similar.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mickey Segal - 20 Nov 2005 16:43 GMT
> I am so glad Java enforces this rule.  Other languages create needless
> chaos by not enforcing something similar.

The rule is not really enforced in a stringent way because you can have 50
classes in one JAVA file; all you need to do is name the file the same as
one of the classes.  Some will argue that one should store all of the
classes in 50 separate files.  While this makes sense in many programming
situations, it is burdensome in others, particularly when multiple classes
are in one inheritance tree and when few programmers are involved with
maintaining that set of classes.
Roedy Green - 21 Nov 2005 01:10 GMT
On Sun, 20 Nov 2005 11:43:06 -0500, "Mickey Segal"
<not_monitored@example.com> wrote, quoted or indirectly quoted someone
who said :

>The rule is not really enforced in a stringent way because you can have 50
>classes in one JAVA file; all you need to do is name the file the same as
>one of the classes.
It has the name of the PUBLIC class.  You can't put two public
classes in the same source file.  Ideally, every top level class
should have get its own source file, public or not, with a matching
name.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.