File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Compiling a blank file?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling a blank file??" Watch "Compiling a blank file??" New topic

Compiling a blank file??

An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
Save a blank file as some java file...say "" and try compiling the file. It compiles. Why shoud it allow? is a blank file a proper source? wats the logic that a blank file a error free code?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

I just tried it: save a file with only a CR, LF in it with the name ".java" and try to compile it like this:

javac .java

This works with JDK 6 on Windows XP, but you don't get a class file.

It's probably just a peculiarity of the Java compiler. The question whether an empty file is a valid Java source file is not terribly interesting. Nobody makes empty Java source files on purpose in real-world projects.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
hi..thnx for the reply..

i m using textpad and j2sdk1.4.2_13 in windows xp..clicked on new and saved it as "". and compiled it. i didnt get any class file...

ne idea?
[ January 17, 2007: Message edited by: Sushant Pradhan ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Is there a particular reason why you need [not] to be able to, compile an empty file? If not, I suggest that there are more interesting questions to concern yourself with.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
well no specific reason but its a basic question which we are all expected to know...if you dont have a answer please shut and make such vague excuses. invest time in something better
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Let's all remember to be nice here.

Whenever someone asks "why?", it's also possible (and sometimes more informative) to ask "why not?". Why shouldn't a compiler allow a blank file to be comiled without error? Yes, they could have written javac so that this causes an error, or they could choose to ignore it. In this case, I believe the latter was simpler. Consider: it's possible for a file to contain any of the following combinations:
  • one public class and no package classes
  • one public class and one package class
  • one public class and several package classes
  • no public class and one package class
  • no public class and several package classes

  • I would summarize this as: a file may contain 0 or 1 public classes, and any number of package classes. A special case of that is that there may be no public and no package classes. That's what you have here. In order to exclude this possibility, one would have to make the rule a little more complicated. Up until now, no one has really cared enough about this rule to make it more complicated. OK?

    "I'm not back." - Bill Harding, Twister
    An Sush
    Ranch Hand

    Joined: Jan 17, 2007
    Posts: 47
    Thnx Jim,
    Agreed was a bit rough.

    Continuing with your explantion why is there a restriction have only 1 or none public class in a file while the restriction doesnt apply to default class.

    Let me make it more clear.
    A file can have only one public class and the file name shd be the public class' name. But this doesnt apply to default class (if public isnt declared). The file can be saved by using either of the default class' name. Is there some reason why this kind of restriction applies to only public classes in a file??
    Jaime M. Tovar
    Ranch Hand

    Joined: Mar 28, 2005
    Posts: 133
    I think it has to do something about the java syntactical/lexical definition. Epsilon, an empty string must be the simplest way of a java program.

    She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
    Ernest Friedman-Hill
    author and iconoclast

    Joined: Jul 08, 2003
    Posts: 24199

    Originally posted by Sushant Pradhan:

    Continuing with your explantion why is there a restriction have only 1 or none public class in a file while the restriction doesnt apply to default class.

    Imagine that you have two files, and containing classes A and B. Furthermore, imagine that class A mentions class B. Now, you type "javac". At some point, the compiler is going to look for B.class, and since it doesn't exist yet, what should the compiler do? Of course, what it does is look for "" and expect class B to be defined in it. If instead contained class C, and contained class B, then the compiler would fail to compile anything.

    So even without the rule about one public class per file, you can see another common sense rule: if a class is referenced outside of the source file in which it is defined, the source file should be named after the class. Many Java compilers will warn about violations of this even for non-public classes.

    Now, the only reason for a class to be public is for it to be used outside of its own source file, right? So it makes sense to make the rule stronger in this case. The common-sense rule, the one that people follow consciously or not, is that a source file should contain at most one class that is ever mentioned by name outside of the file, and the file should be named after that one class.

    [Jess in Action][AskingGoodQuestions]
    I agree. Here's the link:
    subject: Compiling a blank file??
    It's not a secret anymore!