Friday, April 22, 2011

Java source file format

A Java source file has the following elements in this specific order.
  • An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is -
    package testpackage;
  • Zero or more import statements. The import statement makes any classes defined in the specified package directly available. For example if a Java source file has a statement importing the class "java.class.Button", then a class in the file may use Button class directly without providing the names of the package which defines the Button class. Some examples of import statement are -
    import java.awt.*; // All classes in the awt package are imported.
    import java.applet.Applet;
  • Any number of class and interface definitions may follow the optional package and import statements.
If a file has all three of the above constructs, they must come in the specific order of package statement, one or more import statements, followed by any number of class or interface definitions. Also all the above three constructs are optional. So an empty file is a legal Java file.

No comments:

Post a Comment

Chitika