Saturday, 21 August 2010

Java File without a "class" or "interface" in it.

Yes you can write a Java file without a class or interface in it.

This File is "package-info.java"

The code goes as follows for this File:-
==============================================
/**
* documentation comments...
*/
@Annotation1(...)
package sample;

==============================================
Now compile this file...it will compile and will produce class file.But Question remains how it compiles this file and what is the use of this file??

This file is meant for any package level comments and/or annotations.
As Java Files does not support these over Package statement.

When the compiler encounters package-info.java file, it will create a synthetic interface,package-name.package-info. The interface is called synthetic because it is introduced by the compiler and does not have a corresponding construct in the source code. This synthetic interface makes it possible to access package-level annotations at runtime. The javadoc utility will use the package-info.java file if it is present, instead of package.html, to generate documentation.


No comments:

Post a Comment