This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why  file name and class name should always match? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why  file name and class name should always match?" Watch "why  file name and class name should always match?" New topic
Author

why file name and class name should always match?

Mayuresh Rahate
Greenhorn

Joined: Aug 14, 2011
Posts: 7
Hii guys..... what if i wrote something like
IN test.java file-


why is it the convention that class name and file name should be same ..... what is reason behind it???
Need in depth knowledge for it because i am preparing it for interview

[edit]Add code tags, and get rid of text colours, which some people find difficult to read.[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch

CapitalLettersForClassNames, please.

It is a requirement for most compilers that the name of a public top-level class match the file name. It makes for faster compilation of the compiler does not have to "search" every file in the directory to find a particular class.
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

Hi Mayuresh,

According to my understanding, this rule is not applied when there is no public class in the source code file. But when there is a public class, there can be only one Public class in the source code file and the file name must match the class name in that case.
I guess thats done to let JVM know about our public class and hence the entry point.

Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Mayuresh Rahate
Greenhorn

Joined: Aug 14, 2011
Posts: 7
Suhas Mandrawadkar wrote:Hi Mayuresh,

According to my understanding, this rule is not applied when there is no public class in the source code file. But when there is a public class, there can be only one Public class in the source code file and the file name must match the class name in that case.
I guess thats done to let JVM know about our public class and hence the entry point.



but that dosen't make sense even if i follow the rule of one public class per file..... if write a public class with different name and different of file name then also that class will consider as a entry point then what is the use of keeping file name and class name different.. need some thing solid answer.... if you have any links ... please forward it to me .... and thanks for your reply
Mayuresh Rahate
Greenhorn

Joined: Aug 14, 2011
Posts: 7
Campbell Ritchie wrote:Welcome to the Ranch

CapitalLettersForClassNames, please.

It is a requirement for most compilers that the name of a public top-level class match the file name. It makes for faster compilation of the compiler does not have to "search" every file in the directory to find a particular class.


Hey thanks campbell.... i will remember that convention for next time onwards..... regarding your reply.... but ultimately that dose not clear my mind... i just want to know that what will happen if i wrote class name different and file name different.... i sm going into the depth because i am preparing for interview so i must clear my mind before facing some champs at interviewer position
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72


That was just my guess.
Anyways check this discussion on StackOverFlow.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Mayuresh Rahate wrote:... i just want to know that what will happen if i wrote class name different and file name different....
Have you tried it?
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Campbell Ritchie wrote:Welcome to the Ranch

CapitalLettersForClassNames, please.

It is a requirement for most compilers that the name of a public top-level class match the file name. It makes for faster compilation of the compiler does not have to "search" every file in the directory to find a particular class.


Your answer made me think for a while as I always gave a different answer for this question...
I will put a scenario..Let me know if I got you right?

What if I use a class which doesn't have public access, then the compiler needs to search every file in the dir. right?

For example - class A has default access and it instantiates class B which also have default access and both are in the same directory.

Now compiler needs to search for the class B to compile the class A.

how would the compiler know whether class B is public or not? it will ultimately end up searching the whole dir.

according to your answer does the compiler look for B.java first?..is there any hierarchy java compiler follows...???

To test your answer I tried to do something, but ultimately got myself confused...I will put the code here...

in apple2.java
//import foo.Fruit;
class Apple
{
public static void main()
{
System.out.println("i am in small");
}
}


in apple1.java
//import foo.Fruit;
class Apple
{
public static void main()
{

System.out.println("i am in biggggg");
}
}


in kunal.java
//import foo.Fruit;
class kunal
{
public static void main(String a[])
{
Apple a1 = new Apple();
a1.main();
}
}

all 3 are in same dir...after compiling apple1 & 2, if i compile & run kunal...I get apple1 and apple2 sys out depending upon which of them was compiled last totally makes sense...

Now delete the Apple class generated in the directory.
And compile kunal.java it will compile kunal and also apple?
but apple class from apple1 or apple2?

according to what I saw it always picks the file which alphabetically comes first. that means compiler started looking for apple class in all the files, it didn't know whether it was public/default...

And the answer which I used to give, I remember reading it somewhere, may be TIJ -

It is more of a design issue. A java file is considered as a bucket of classes to achieve some task. Now you should have one bucket for each task and if you have only one main task for source file - then only one public face of the file should represent it. and hence - one public class per file. you can have any number of classes as you want. And they will be given default package access. you can consider a package a bigger bucket containing other buckets. so that totally makes sense being accessible to other ones.

I hope this forum will make my painful preparation a little bit easier...




kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Campbell Ritchie wrote:Welcome to the Ranch

CapitalLettersForClassNames, please.

It is a requirement for most compilers that the name of a public top-level class match the file name. It makes for faster compilation of the compiler does not have to "search" every file in the directory to find a particular class.


Ok..I did some research and found explanation for your answer...The following Thread..

http://www.coderanch.com/t/409755/java/java/why-only-one-public-class

As pointed out in the JLS -

When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

* The type is referred to by code in other compilation units of the package in which the type is declared.
* The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.


Now I get the 2nd point...
What about the 1st one...What I did was exactly the same...

So till now what I understood is this -

If the referenced class is having a fully qualified name - then the compiler will look for it as specified...
And it it is not having fully qualified name - then the compiler would look for in the same package/dir - searching through every files for a class definitions if there is not a class file present already..This part is proved by the code I gave...

Need some experts to comment on it..
 
 
subject: why file name and class name should always match?
 
Similar Threads
Why the name of the public class should be the name of the file as well
public class
why should the class name and the notepad name should be same?
why do we need to save the java file with the name of public class file
public class...