• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiling a blank file??

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Save a blank file as some java file...say "aa.java" 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..thnx for the reply..

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

ne idea?
[ January 17, 2007: Message edited by: Sushant Pradhan ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
     
    An Sush
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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??
     
    Ranch Hand
    Posts: 133
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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, A.java and B.java containing classes A and B. Furthermore, imagine that class A mentions class B. Now, you type "javac A.java". 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 "B.java" and expect class B to be defined in it. If B.java instead contained class C, and C.java 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic