tags:

views:

86

answers:

4

Except classes and interfaces is there anything else we can write in a java source file? because what ever .java file i have seen will have either class or interface in it.

+7  A: 

You can also define enums

alasdairg
Which is just a class.
danben
Yep, its just syntactic sugar and defines a class behind the scenes
alasdairg
It's all syntactical sugar for a Turing machine. enums are not just classes.
Tom Hawtin - tackline
A: 

Yes, you can also write comments.

Carl Smotricz
You could write a .java file containing only comments, but I doubt there would me much point using a .java file for something that won't produce any output when you could just use a .txt or .rtf file instead. That doesn't make your post any less true though!
TabbyCool
@TabbyCool: The point isn't the point. What can you...? Classes, interfaces, enums and comments are all valid answers, as is "nothing". @downvoter: I'd appreciate hearing what you found wrong about my answer.
Carl Smotricz
+4  A: 

Aside from classes, interfaces, and enums, one can write a package-info.java which contains the Javadoc comments and annotations for a package.

There is some information regarding package-info.java in Section 7.4.1.1: Package Annotations in The Java Language Specification, Third Edition.

The use of package-info.java for package documentation is supported from the Javadoc tool which was shipped in Java 5. The What's New in Javadoc 5.0 page has an entry on the support for package javadocs.

Edit: Added information regarding support for annotations per Tom Hawtin - tackline's comment.

coobird
And package annotations.
Tom Hawtin - tackline
+3  A: 

Nothing!

According to the JLS a completely blank file is a valid .java file. If you create an empty .java file and compile it, it compiles without error, but doesn't produce a .class file.

Don