f



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

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

0
11/20/2005 6:00:03 AM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

6 Replies
628 Views

Similar Articles

[PageSpeed] 37

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=B4t 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=B4t 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 pleas=
e Ignore it
> Once again i am telling i am new to java platform....
> OK Thank you...
You=B4re welcome. But now stop philosophising and write some code! There
are MANY MORE questions waiting for you...
Good luck
Piet

0
pit.grinja (85)
11/20/2005 8:24:53 AM
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

0
ameyas7 (7)
11/20/2005 10:47:10 AM
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.

-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
11/20/2005 2:33:07 PM
On 20 Nov 2005 02:47:10 -0800, ameyas7@yahoo.com wrote, quoted or
indirectly quoted someone who said :

>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.

-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
11/20/2005 2:34:22 PM
"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid> wrote in 
message news:l221o1p2c57rdegs4fco1nr5shlan7bqub@4ax.com...
> 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.


0
11/20/2005 4:43:06 PM
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.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
11/21/2005 1:10:29 AM
Reply: