Communiquez avec les autres et partagez vos connaissances professionnelles

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

Suivre

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

user-image
Question ajoutée par srivatsan murali
Date de publication: 2016/03/15
Waheeb Al-Rafati
par Waheeb Al-Rafati , Software Development Engineer , Lead Technologies Inc

No, unless your class is a public class

Naeema Rabia
par 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
par 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