• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why file name and class name should always match?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mayuresh Rahate
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That was just my guess.
Anyways check this discussion on StackOverFlow.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

https://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..
 
Don't play dumb with me! But you can try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic