Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Does the class name and the file name of the java program be same ? If yes why ?

user-image
Question added by srivatsan murali
Date Posted: 2016/03/15
Waheeb Al-Rafati
by Waheeb Al-Rafati , Software Development Engineer , Lead Technologies Inc

No, unless your class is a public class

Naeema Rabia
by Naeema Rabia , Clinical Informatics Support Specialist , Cleveland Clinic Abu Dhabi

NO, it is not mandatory to keep the filename same as the class name. However, it is advisable because suppose we have a program with more than1 class, in that case after compiling the Java source file, a number of .class files will be generated.In this condition you will not able to easily identify which class need to be interpreted by java interpreter and which class containing Entry point.

Suppose ClassA is the class name and this class has the main() method. In that case,

Filename should be ClassA.java

 

Steps of execution:

javac ClassA.java

java ClassA

 

muqaddar raza
by muqaddar raza , Senior Software Engineer , i2cinc

 

No, Class name and file name of Java Program should not necessary to be same if class name doesn't have the public modifier.

If Class name has public modifier then Java file name must be same otherwise compiler error.

More Questions Like This

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