Wednesday, December 19, 2018
Home > java > General structure of Java Program

The general structure of Java Program

In order to learn any programming language. We must know the very basics of programming to move further to more complex things in it.  The general structure of the program written in the language in one of those basics. A simple java program consists of the following:

  •      Documentation
  •      Package statement
  •      Import Statements
  •      Class Definition
    •           Main Method Class
      •                Main Method Definition

Documentation: –

Documentation section is the very first section in the java program. Not everyone uses this section and you may not see this portion in any of the programs you see around but this section can be very helpful in many ways. Such as you can write comments in this section for your program. These types of comments are newer in java. And they start with /** and ends with */.

/** Author: www.codeslehrer.com

    Date: 2018-10-04

    Description:… */

 

The Javadoc utility program reads these comments from the program and uses them to prepare the program’s documentation.

Package statement: –

Usually, this is the very first line in the java program as many users omit the documentation section. This statement declares the package information about the class. The package in which the class resides and it is the portion in which the data members and member function with package access specifier are accessible.

E.g.

 package example;

Import Statements: –

Import statements are used to import different packages and classes in the class. These are useful when we want to use some other classes available in some other package or some other functions present in those classes.

 

E.g.

import java.io.IOException;
import java.math.BigDecimal;

Class Definition: –

After the import statements, we have the class definition which includes:

  • Access specifier for class
  • Class keyword
  • Name of the class.
  • Class body.
public class ClassName
{
    // class body.
}

Class Body: –

The class body can be further elaborated in a way that it contains data members and member functions. Data members are the variables or constants or objects created in the class. And member functions are the functions that user manipulates these data members though it is not necessary that these member functions must use these data members.

Main Method Class: –

It is the class which contains the main function in it.

Main Method Definition: –

The function from which the java program starts. Sometimes referred as program driver.

 

public class ClassExample {
// main method class.
       public static void main(String[] args) {
              // function from which the java program starts its execution.
       }
}

Below is the image indicating the different portion in the general structure of java program.
General structure of java program

Simple java program example

Let’s have a look at a very famous programming example of hello world.  A simple java program to print hello world on the screen.

package example;

/** Author: www.codeslehrer.com
Date: 2018-10-04
Description:
This program is an example of a simple java program
printing hello world on the console. */

public class Helloworld {
       public static void main(String[] args) {
              System.out.println("Hello World");
       }
}

 

Output:

hello world a simple java program example

Some other basics about the general structure of Java program are:

A Java class name should start with a capital letter.

The java class name should be the same as the file name and each java class must be saved with a .java extension.

Java is case sensitive language. So if we don’t use uppercase and lowercase properly our program will result in the syntax error.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: