
Can we run a Java program having class name and file name different?

Hi Friend,
No, you can't. It is necessary to save the java file with its class name otherwise it will give error.
Thanks

File name : Sample_File.java
class Sample { public static void main(String args[]) { System.out.println("This is a program with class name and file name are different"); } }
when I run this program as javac Sample_File.java java Sample
I am getting the output without any error.


From Compiler point of view it is only the class name that matters rather than host specific source file name.
Normally, java compiler creates the .class file of any java file with its class name and .class extension. i.e., If we have a java source file with name 'Sample123.java' and the class name in that file is 'Sample' then the compiler would create a class file with name 'Sample.class'.
Java Specification:
NormalClassDeclaration: ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt ClassBody
The Identifier in a class declaration specifies the name of the class.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.