Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by Jayakumar P
Date Posted: 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
by 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
by 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

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.