• Java Program Structure

    Java Program Structure

    In Java Program Structure,  java program may contain many classes of which only one class defines a main method. Classes contain data members and methods that operate on the data members of the class. Methods may contain data type declaration and executable statements.

    To write a Java program, we first define classes and then put them together. A java program may contain one or more sections as shown following figure.

    Java Program Structure Image

    Figure: Java Program Structure

    Documentation Section :-

    The documentation section comprises a set of comment lines giving the name of the program, the author and other details, which the programmer would like to refer to at  a later stage. Comments must explain why and what of classes and how of algorithms. This would greatly help in maintaining the program. in addition to the two styles of comment like “single line comment” and “double line comment”, Java also uses a third style comment /**…..*/ known as documentation comment. This form of comment is used for generating documentation automatically.

    Package Statement :-

    The first statement allowed in a Java file is a package statement. This statement declares a package name and informs the compiler that the classes defined here belong to this package.


    package student;

    The package statement is optional. That is, our classes do not have to be part of a package.

    Import Statement :-

    The next thing after a package statement (but before any class definition) may be a number of import statements. This is similar to the #include statement in C. Example,

    import student.test;

    This statement instructs the interpreter to load the test class contained in the package student. Using import statement, we can have access to classes that are part of other named packages.

    Interface Statement :-

    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 the multiple inheritance feature in the program.

    Class Definitions :-

    A Java program may contain multiple class definitions. Classes are the primary and essential elements of a 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 problem.

    Main Method Class :-

    Since every Java stand-alone program requires a main method as its starting point, this class is the essential part of Java program. A simple Java program may contain only this part. The main method creates objects of various classes and establishes communications between them. On reaching the end of main, the program terminates and the control passes back to the operating system.

    Java Example 

    Implementation of a Java Program involves a series of steps. The include.

    1. Creating the program
    2. Compiling the program
    3. Running the program.

    Remember that, before we begin creating the program, the Java Development Kit (JDK) must be properly installed on our system.

    1. Creating the Program

    We can create a program using any text editor. Assume that we have entered the following program: 


    import java.lang.*; 

    import java.io.*; 

    class Test { 

         public static void main(String [] args) { 


              System.out.println(“Welcome to the world of Java.”); 

              System.out.println(“Let us learn Java.”); 



    We must save this program in a file called Test.java ensuring that the filename contains the class name properly. This file is called the source file. Note that all Java source files will have the extension .java. note also that if a program contains multiple classes, the file name must be the class name of the class containing the main method.

    2. Compiling the Program.

    To compile the program, we must run the Java compiler javac, with the name of the source file on the command line as shown below: 

                                             javac Test.java 

    if everything is OK, the javac compiler creates a file called Test.class containing the bytecode of the program. Note that the compiler automatically names the bytecode file as <classname> .class

    3. Running the program.

    We need to use the Java Interpreter to run a stand-alone program. At the command prompt, type 

                                             java Test 
    Now, the interpreter looks for the main method in the program and begins execution from there. Note, that we simply type “Test” at the command line and not “Test.java” or “Test.class”.

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *