Register now or log in to join your professional community.
No, unless your class is a public class
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
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.