Relationship between filename and classname

P

Prasoon

Hi I am new to java.I want to know is there any specific reason for
the class-name in java being same as the file-name????
I know that we can save the file with a different name than the class-
name except when the class is public....but during execution we have
to give the name of the class......

But why in most of the cases file-name is same as class-name.????
Is there any specific reason for the same or just the designers of
java said so that's why????
 
R

RedGrittyBrick

Prasoon said:
Hi I am new to java.I want to know is there any specific reason for
the class-name in java being same as the file-name????
I know that we can save the file with a different name than the class-
name except when the class is public....but during execution we have
to give the name of the class......

But why in most of the cases file-name is same as class-name.????
Is there any specific reason for the same or just the designers of
java said so that's why????

I believe it is just a convention which the original compiler writers
found useful and is not mandated in the JLS:
http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html

Speaking[1] of conventions, normally we use punctuation marks singly!
 
L

Lew

Prasoon said:
Hi I am new to java.I [sic] want to know is there any specific reason for
the class-name in java [sic] being same as the file-name????
I know that we can save the file with a different name than the class-
name except when the class is public....but during execution we have
to give the name of the class......

But why in most of the cases file-name is same as class-name.????
Is there any specific reason for the same or just the designers of
java said so that's why????

As others have pointed out, it is a convention, albeit a nearly
universal one.

The reason for the convention is organizational convenience. Because
directory structure parallels package structure and class names match
file names, it's easier to keep track of packages and classes on a
file system than if they didn't match.

Package.class: com.lewscanon.foo.Foo
directory/file: com/lewscanon/foo/Foo.class

Note that directories are relative to the class path, that is, to one
entry in the classpath. Thus if I were to execute (on Linux/
Solaris/...):

java -classpath /opt/projects:/usr/jcode:. com.lewscanon.foo.Foo

then the class would be in the first one found of the locations:

/opt/projects/com/lewscanon/foo/Foo.class
/usr/jcode/com/lewscanon/foo/Foo.class
./com/lewscanon/foo/Foo.class

The source code tree would have a cognate structure, with Foo.java
instead of Foo.class.

/home/lew/projects/foo/src/com/lewscanon/foo/Foo.java

The Javadocs for Class#getResource() and getResourceAsStream() touch
on this.

The rule of thumb is that a dot (.) in the package is a slash (/) in
the path.

Other relevant conventions are that packages have names in all lower
case, and class names start with an upper-case letter and have mixed
(camel) case, the first letter of each compound word part being
capitalized and the rest lower case.

Bear in mind that packages are not hierarchical. Packages are
namespaces, and there is no inherent connection between packages with
common name parts. That is, package com.lewscanon.foo.bar is not
inherently related to package com.lewscanon.foo except
psychologically.
 
R

Roedy Green

Hi I am new to java.I want to know is there any specific reason for
the class-name in java being same as the file-name????

Yes. Java's naming system lets java find the corresponding source or
class file given only the name of the class. The compiler can then
figure out what sort of code needed to invoke a method since it can
get the details from the class file, or if it is missing my compiling
the class file. Make sure you get the case right too.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan
 
A

Arne Vajhøj

Prasoon said:
Hi I am new to java.I want to know is there any specific reason for
the class-name in java being same as the file-name????
I know that we can save the file with a different name than the class-
name except when the class is public....but during execution we have
to give the name of the class......

But why in most of the cases file-name is same as class-name.????
Is there any specific reason for the same or just the designers of
java said so that's why????

It is much easier to find the source for a given class that way.

Besides, try and consider the implications for classloaders and
jar files if it was not the case. It would become rather messy.

Arne
 
M

Mike Schilling

Arne said:
It is much easier to find the source for a given class that way.

Besides, try and consider the implications for classloaders and
jar files if it was not the case. It would become rather messy.

That is, it's important because Java loads classes at runtime, and
needs to be able to find them easily. If Java were a statically
linked language like C or C#, where you specify the set of files used
to build an executable, there would be no strict requirement for the
class-name convention. (Though most developers still follow something
like it, so that humans can navigate through the source code.)
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top