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

C

corejavagroups

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
 
P

pit.grinja

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
 
A

ameyas7

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
 
R

Roedy Green

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

Roedy Green

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

Mickey Segal

Roedy Green said:
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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top