Java is a case sensitive programming language. To be able to write a program in Java, it is necessary to know the exact structure of a Java program. So, let us study the structure of a Java program along with its important syntax and keywords.

Basic Structure of a Java Program

A Java program involves the following sections some of which are compulsory and others are optional. These are:-

  1. Documentation Section
  2. Package Section
  3. Import Section
  4. Class Definition
  5. Main Method Class

Documentation Section

It is an optional section. It is used to write any number of comments in the program. These comments help a programmer to understand the code in an easier manner.  It is written to define the purpose and the action being taken in different steps of the program.

There are two ways to provide comments. These are:

  1. // comment to be written :           used for a single line comment.
  2. /* comment to be written */ :           used for comments which occupy multiple lines.

Package Section

Package is a way of grouping related classes and interfaces that are defined by a common name. It is an optional section. Package keyword is used to declare a package which tells the compiler that the class used in the program belongs to a particular package. It is declared as:

package package_name;

For example: package MyPackage;

Import Section

This statement instructs the compiler to load particular classes that are defined under a particular package. This is also an optional section. This is done when you want to use a class of another package. To do so you have to make use of the import keyword.

For example: import java.lang.*;

Class Definition

Classes are one of the main and essential parts of any Java program. This is a compulsory statement as this section is used to define a class for the program. A Java program may contain many class definitions depending on the requirement of the programmer.

In order to define a class the class keyword is used followed by the class_name, starting with curly braces ‘{‘and ending with closing curly braces ‘}’.

For example:

class Welcome {

…. class body ….}

Main Method Class

The essential part of  Structure of a Java Program is main method since it is the starting point of the program. It is a compulsory section, which tells the compiler about the beginning of the execution of the program. If we are defining multiple classes in our program, only one class can define the main method.

A class may have several methods including main().  When Java interpreter starts execution of the program, it starts from main() and it calls all the other methods required to run the application from there. The syntax to define main() is as follows:

public static void main(String args[]) {

… body of main() …}

  • The word public means that it can be used by code outside this class.
  • The word static is used when we want to access a method without creating its objects.
  • The word void indicates that the method does not return any value.
  • The word main specifies the starting of the main method.
  • The words String args[] are used to specify input parameters when the program is run through the console.

Example of a simple Java program

// Name this file
public class Welcome
/* This is a simple Java program
It will print a simple message on screen */
public static void main(String args[])
            System.out.println(“Welcome to the world of Java”);

Program output:

Welcome to the world of Java