I have a trivial question to ask you. But i am not able to get a convincing
or understanding
answer to this question. Why must the name of the file must be the same of
that of the public
class it contains? And why is there a restriction that only one public class
file must be present
in a file. I think these questions are inter-related.

Thanks in advance.

-Siva

01-03-2001, 07:42 AM

Brett Hicking

Re: file name==class name. why?

Hi,

Generally you will always have a public class that has the same name(exactly,
as java is case-sensitive) as the file it resides in and it is common practice
to include the name of the file in a comment at the first line of the file.
e.g...

When I first started out in Java I thought the reason for this was to tell
the compiler where to find the main method that would specify the application
execution start point when you run it by typing java PublicClassName but
in .java source files each class can still have it's own public static void
main method(even non public classes) and when the .java source file gets
compiled each class is created as a separate bytecode file and you can run
a .class file on it's own if the class contains a main method in the source
file. I'm not entirely sure why you have to name them rhis way other than
helping the programmer while developing as a developer can look at a source
file and see from the name of the file that the class of most interest is
the class with the name of the file, the public and user creatable one.
If you find out more let me know.

The restriction of only one public class per source file is Java's way of
sort of forcing you into organising/splitting your application up into logical
units. Each public class in a file is the portion visible to the outside
world and the user can make instances of that class, all other classes contained
in the file should be support classes to the public class and not user creatable
thereby making each source file its own functional unit. These files can
also be grouped by using the package keyword, this allows you to group a
number of class files into libraries of related classes. The way java makes
you organise these classes also helps to prevent name clashes.

hope that helps

Brett
"siva" <siva_mrs@usa.net> wrote:
>
>hello all,
>
> I have a trivial question to ask you. But i am not able to get a convincing
>or understanding
>answer to this question. Why must the name of the file must be the same
of
>that of the public
>class it contains? And why is there a restriction that only one public class
>file must be present
>in a file. I think these questions are inter-related.
>
> Thanks in advance.
>
> -Siva
>

01-03-2001, 10:40 AM

Paul Clapham

Re: file name==class name. why?

siva <siva_mrs@usa.net> wrote in message news:3a520a57$1@news.devx.com...
>
.... Why must the name of the file must be the same of that of the public
class it contains? Thanks in advance.
>
> -Siva
>
When you compile a source file into a class, the compiler will do you a
favour and also compile all classes that your source file refers to, if
their .class file is older than their .java file. To do this, it needs to
be able to identify the source code for a .class file, and hence the naming
convention.

This is actually a very useful thing for the compiler to do. For example,
if class A refers to class B and class B refers to class A, then there would
be no way to compile this pair of classes when you first wrote them unless
the compiler could deal with more than one source at a time.