When I compiled an empty Java file it didn't produce any class file. So I want to know how compiler react when compiling an empty Java file? I thought it should generate an empty class file in this case, but it did not. So why it didn't?
Yes. An empty . java file is a perfectly valid source file.
Can I have an empty class? Is it bad programming practice? And if so, what can I implement instead? "Can I have an empty Java class?" - Yes.
Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.
what do you mean by without any name there need to be some name. Yes,it is possible to compile a java source file with different file name but you need to make sure none of the classes defined inside are public... when you compile the source file the corresponding .
javac
starts, sees there is no class declared in the file, and finishes. In order for a .class
file to be created you must at least have the class declaration in the file.
What most of the answers are saying is really that a class file is not a compiled java file but a binary representation of a class.
Compiling a java file could result in two class files if the java file contains two classes (although only one can be public) and that is why compiling something with zero classes will result in zero class files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With