• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Confirmation on files without class or interface definitions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I just wanted to check if both of these assertions are true:
"A Java file without any class or interface definitions can also be compiled."
"If an import statement is present, it must appear before any class or interface definitions."
Thanks very much for your time,
Trav
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"A Java file without any class or interface definitions can also be compiled."


From JLS 7.3:
CompilationUnit:
PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)

As above, since all the parts of a CompilationUnit in Java is optional, I think it is right to say so. Actually, JDK1.4.1 does treat an empty file as a valid compilation unit. You can even write like this:

It's also a valid compilation unit according to the definition.


"If an import statement is present, it must appear before any class or interface definitions."


Yes. you can see from the above definition that ImportDeclarations must appear before TypeDeclarations.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic