articles

Home / DeveloperSection / Articles / Structure of Java Source Files and Import Statements

Structure of Java Source Files and Import Statements

Allen Scott 6482 16-May-2016

Source File Layout

We have learned enough from the examples given in previous posts so far to get started writing Java programs. Now, let’s discuss the complete structure of a Java source file. When we write a Java source program, it needs to follow a certain structure or template. This template is shown here:
/**
* NewClass.java
*/
package java application;
import classes;
public class NewClass
 {
         public NewClass() {
       }
}

The import statement makes the declarations of external classes available to the current Java source program at the time of compilation.

As stated earlier, we may use any text editor for writing a Java source program. The entire program may consist of more than one source file.

·    Each Java source file must have the same name as a public class that it declares.

·    Each Java source file can contain only one public class declaration.

·    The file extension must be .java.

·    The filename is case-sensitive. Therefore, the preceding source code must be stored in a file named NewClass.java.

·    The source file may contain more than one class declaration; however, not more than one such declaration can be public.

The source consists of three major sections:

·         The package

·         The import

·         Class definition—besides the comments, which we may embed anywhere in the source.

A multiline comment is shown at the top of the example structure, which shows the name of the file under which this code must be saved. 

The import Statement

Immediately following the package declaration, we have import declarations. We use the import statement to tell the compiler where to find the external classes required by the source program under compilation. The full syntax of the import statement is as follows:

import packagename;

or

import packagename.* ;

Here are a few examples of the import statement:

·         import mypackage.MyClass;

·         import mypackage.reports.accounts.salary.EmployeeClass;

·         import java.io.BufferedWriter;

·         import java.awt.*; 

1.    The first statement imports the definition of the MyClass class that is defined in the mypackage package.

2. The second statement imports the definition of EmployeeClass belonging to the mypackage.reports.accounts.salary package.

3.   The third statement imports the JDK-supplied BuffferedWriter class belonging to the java.io package.

4.    The fourth statement imports all the classes belonging to the java.awt package. Note that the asterisk (*) in the fourth statement indicates that all classes are included.

As the syntax suggests, we may import a single class or all the classes belonging to a package.

·      To import a single class, we specify the name of the class

·       To import all classes we specify *.

 

One of the important things here to notice that, the import statement specifies the path for the compiler to find the specified class. It does not actually load the code, as is the case with an #include statement in C or C++. Therefore, the import statement with * does not affect the application’s runtime performance.


Updated 29-Jan-2020

Leave Comment

Comments

Liked By