Why should we give the same name.............

C

corejavagroups

In Java suppose we have a class with class name Example.
In the class we have written the main() method.
when saving the file that contains the source code of class Example
why should we give the name of the file as same as the name of the
class that is as Example.java ?
 
R

Roedy Green

when saving the file that contains the source code of class Example
why should we give the name of the file as same as the name of the
class that is as Example.java ?

so that the compiler can easily find the source code or class files
if some other class references that class.. It needs to see how the
the referenced class is defined to generate code that uses it.
 
T

Thomas Hawtin

Roedy said:
so that the compiler can easily find the source code or class files
if some other class references that class.. It needs to see how the
the referenced class is defined to generate code that uses it.

And so that us humans stand a chance of finding the right file (in a
sensible period of time and with minimal effort).

Tom Hawtin
 
R

Roedy Green

And so that us humans stand a chance of finding the right file (in a
sensible period of time and with minimal effort).

one thing I find odd is that package scope classes can piggyback on
the source code of a public class. There is no easy way for javac to
find them. It must go looking in all the *.java classes in a package
directory.
 
R

Ross Bamford

one thing I find odd is that package scope classes can piggyback on
the source code of a public class. There is no easy way for javac to
find them. It must go looking in all the *.java classes in a package
directory.

I believe that it uses the name of the classes anyway - if you compile a
source file with three classes, two non-public obviously, you get three,
properly named class files. I expect it builds up that mapping first, and
then uses that from there.

AFAIR even 'back in the day' the source naming thing was motivated more by
consistency and readability (and maybe an easier time for implementors)
than the need for javac to find sources.
 
L

Luc The Perverse

Ross Bamford said:
I believe that it uses the name of the classes anyway - if you compile a
source file with three classes, two non-public obviously, you get three,
properly named class files. I expect it builds up that mapping first, and
then uses that from there.

AFAIR even 'back in the day' the source naming thing was motivated more by
consistency and readability (and maybe an easier time for implementors)
than the need for javac to find sources.

Absolutely. This was a deliberately introduced system to encourage better
styles of organization.
 
T

Thomas Hawtin

Luc said:
Absolutely. This was a deliberately introduced system to encourage better
styles of organization.

Come to think of it, allowing package private outer classes made more
sense in JDK 1.00. With no nested classes, every class was an outer
class. If you had lots of small classes involved with a single class
implementation then there could have been huge numbers of tiny files,
mostly consisting of import statements.

Tom Hawtin
 
L

Luc The Perverse

Come to think of it, allowing package private outer classes made more
sense in JDK 1.00. With no nested classes, every class was an outer class.
If you had lots of small classes involved with a single class
implementation then there could have been huge numbers of tiny files,
mostly consisting of import statements.

You are not implying that that is better though?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top