Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you compile an empty java file?

Tags:

java

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?

like image 841
GuruKulki Avatar asked Mar 18 '10 05:03

GuruKulki


People also ask

Is Empty Java file valid?

Yes. An empty . java file is a perfectly valid source file.

Can a Java class be empty?

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.

What does compiling a Java file do?

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.

Can we have a Java file without name?

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 .


2 Answers

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.

like image 177
Amir Afghani Avatar answered Sep 27 '22 20:09

Amir Afghani


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.

like image 26
Fredrik Avatar answered Sep 27 '22 20:09

Fredrik