Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty file is a valid Java source file. How it is handled inside the JVM?

Tags:

java

jvm

Empty file is valid Java source file, but how it is handled inside the JVM?

like image 800
srini Avatar asked Oct 17 '11 08:10

srini


1 Answers

There is no one-to-one relation between .java-files and .class-files. What you have is a one-to-one relation between classes (or class-declarations) and .class-files.

A Java-source file with zero class-declarations will not result in any .class files, so it's not really handled by the JVM at all.

$ touch Test.java

$ ls
Test.java

$ javac Test.java

$ ls
Test.java

In the Java Language Specification a Java-source file is synonym with a Compilation Unit. The relevant section in the JLS is 7.3 Compilation Units.

The grammar is described as follows:

CompilationUnit:
     PackageDeclarationopt ImportDeclarationsopt TypeDeclarationsopt
...

The opt-subscript says that the part is optional. Since TypeDeclarations is optional, no class-declarations need to exist.

like image 95
aioobe Avatar answered Sep 24 '22 20:09

aioobe