Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Why we need the java file name and public class name in that file as same ?

user-image
Question ajoutée par Jayakumar P
Date de publication: 2014/03/10

It is not mandatory to make the file name and class name same but if you make it then it will increase the readability of the code.

Shruti Srivastava
par Shruti Srivastava , Guest researcher , PRI Wageningen University

I was about to say, that it is simply a must. But I looked at the JLS, and it is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.

Practically spoken - common compilers do have that restriction, it's much easier for the compiler to find a compilation unit or for a classloader to find a class file with such a restriction in place.

Shadab Hussain
par Shadab Hussain , Software Engineer , Odesk

Hello JayaKumar,I think this is simply a convention which helps JVM to find the Source Files.This restriction is not by Java Language Specification, its compiler's duty to handle souce code in files. common compilers do have that restriction, it's much easier for the compiler to find a compilation unit or for a classloader to find a class file with such a restriction in place.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?