Java program Structure

In any language we need to follow specific structure to write the program. A java program structure divided into various sections. Some of them are mandatory and some are optional. A java program contain many classes of which only one class is defines a main method. Classes contain data members and some methods that operate on the data member of the class. the method contain data type declarations and executable statements. To write java program, Firstly we need to define the class and put them together.

Documentation Section
Package Statement
Import Statements
Interface Statements
Class Definitions
Main method class
Java Program Structure

Documentation Section

The documentation section comprises a set of comment line that improve the readability of the program. A comment is a non-executable statement that helps to read and understand a program, as well as explain why and what of classes and how of algorithms . A good program should include comments that give us some information like name of program and author of program etc.

Three type of comments as below.

Single line Comment: Start with a double slash symbol(//) and terminate at the end of the same line. For Example

// Comment lines

Multiline comment: It start with /* and ends with */ .For example

/* Calcuates

sum of six numbers */

Package Statements

The first statement of the source code file is package . It tells the compiler that classes defined in the file belong to this package and also provides a package declaration. this statement is optional .It must appear in the java file before any declaration of class.

For Example:

Package student;

In the above statement, declare that all the classes and interfaces defined in the java file are the part of student.

Import Statements

An import statement allow us to access a class which belongs to the other package. Import statement is written after the package statement and before the any class definition.

For example

Import student.SName;

Here, Student is the name of the package and SName is the class which we want to access, and we also can access the classes that are part of other Named Package.

Interface statement

An interface is similar to a class but includes a group od constants and method declaration. it is an optional section and is used when we want to implement multiple inheritance in java.

interface book
    void push(num item); // insert item into book
          num pop(); // delete an item from book

Class Section

This section is used to describes the information about the user-defined classes in the program. A java program may contain multiple classes definition. In every java program contain at least on class. A class is a group of data variables and methods.

Main method class

This section , contain the main() method where from the program is actually starts. In the main method of the class, the objects of various classes can be created, access and manipulated.

class Firstjavaprog // Class name is firstjavaprog
   pubilc static void main(Strings[] args)
     system.out.printIN("My first java program");

Program Explanation

  1. Defines the class named Firstjavaprog. the keyword Class before the classname identifies that it is a class definition.
  2. Every class definition in java starts with open brace ” { ” and ends with closing brace ” } “.appearing in the last line .
  3. The main() method of the Firstjavaprog class which serves as the entry point for the program execution. The main method header always written as,
public static void main.

public : The keyword public is an access specifier that declares the main method as unprotected and therefore making it accessible to all other classes.

Static: The keyword static allow main() to called before an object of the class. The main must be declared as static since the interpreter uses this method before any objects are created.

Void: The type modifier void indicate that the main() method does not return any value but simply prints some text to the screen.

String[] args: The String[] args is only the parameter in the main method.

4. The output line , this only contains one executable statement.

system.out.printIn("My first java program");

system: It is contained in the package java.lang. In java, java.lang in impoerted by default.

out: The object out represents the output steam and is a static data member os the class system.

printIn(): The printIn() is the method of out object, that takes the text string as an argument and displays it to the standard output i.e. monitor’s screen.

5. The semicolon at the end of the this statement indicate the end of the statement. Every statement in java is ends with semicolon(;).

Write a Java program to read and display any 10 numbers

import java.util.Scanner;

public class Main

    public static void main(String[] args)
        for(int i = 1; i <= 10; i++)



Recommended posts:

How are java objects stored in memory?


programming with java By balagurusamy

Leave a Comment