Tuesday 8 March 2011

Structure of java program

A Java program may contain many classes of which main() is defined in only one class. Classes contain data members and methods that operate on the data members of the class. Methods may contain datatype declarations and executable statements. To write a java program, we first define classes and then put them together. The different sections of a java program are shown in the below figure:


                                           Structure of java program

Document Section:
This section comprises a set of comment lines giving the name of the program, the author and the other details which the programmer would like to refer to at a larger stage. Comments must explain why and what of classes and how of algorithms. This would greatly help in maintaining the program.

Package Statement:
Package statement is the first statement aloowed in java. This statement declares the package name and informs the compiler that the classes defined here belong to this package.

Example:
package course;

The package statement is optional i.e., our classes may or may not be a part of package.

Import Statements:
After a package statement and before any class definition , may appear a number of import statements.

Example:
import course.selection;

This statement instructs the interpreter to load the selection class combined in package course. We can access classses that that are part of other named package susing import statements.

Interface Statements:
An interface is like a class but includes a group of method declarations. This is also an optional section and is used only when we wish to implement multiple inheritance features in the program.

Class Definitions:
A Java program may contain single or multiple class definitions. They are the primary and essential elements of java program. These classes are used to map the objects of real-world problems. The number of classes used depends on the complexity of the program.

Main Method Class:
This class is essential part of a java program since every java stand-alone program requires a main method as its starting point. The main method creates objects of various classes and establishes communication between them. The program terminates on reaching the end of main, and the control passes back to the operating system.




No comments:

Post a Comment