Java Program Structure

JAVA Programs are essentially constructed in the following manner, as several well-defined sections.

Documentation Section  /* Contains the name of the program, author, and revision number and is rarely used */

Package Section  /* Contains package statements and is optional */

Import Section /* Contains import statements and is optional */

Interface Section /* Contains interface statements and is optional */

Class definition Section /* Contains the class definition of classes other than the main method class */

Main Method Class
    public static void main(String args[])
} /* main() method class definition and is essential */


A Java program may contain many classes of which only one class defines the main() method. But a Java program may contain one or more sections too, in addition to the main() method class definition these are:

  • Documentation Section: This section describes the program name, author name, date, and purpose of the program. All descriptions are written in comments and are ignored by the compiler. This information is very important in program maintenance.
  • Package Section: This section contains the package statement. It is the first statement allowed in the Java program. The package statement informs the Java compiler that the classes defined in this file belong to which package. This statement is optional.
  • Import Section: This section contains one or more import statements. In Java import statement allows the programmer to use one or more classes from other packages. The use of this statement helps the programmer to organize his program in various packages and also he can use various system classes that are an integral part of JDK. For example
    • import java.applet.*;
    • This statement will be discussed further in more detail.
  • Interface Section: An interface is like a class with the exception that it can include only final variables and abstract method declarations. The methods declared in the interface are implemented in the class in which the interface is implemented. This section is also optional and will be discussed further in detail.
  • Class Definition Section: Java program can contain multiple classes in a file out of which one can be a public class. This section defines one or more non-public classes used by the program. This section is also optional.
  • Main Method Class Definition Section: This is an essential section in stand-alone programming. In Java, program execution starts from the main() method, and the main() method must be present in a class. The class containing the main method may or may not be a public class but it should not be a private class. The main logic of the program is implemented in the main method. On reaching the closing brace of the main method the program execution terminates.

Check the following program

class MainMethod {
    public static void main(String[] args) {
        System.out.println("This is a main method"); 
        System.out.println("I must be present in the program"); 

Program Output:

This is main method
I must be present in the program

A Sample ‘Java’ Program

class First {
    public static void main(String[] args) {
        System.out.println("I am learning JAVA Language"); 

Sample program Output:

I am learning JAVA Language

The above program is the simplest of all Java programs. In the above ‘JAVA’ program, uppercase and lowercase characters are very important. The starting point of the Java program is identified by the word:

      class First

This keyword ‘class’ declares a class, which is an object-oriented construct. As discussed earlier, Java is a true object-oriented language, and therefore, everything must be placed inside a class. ‘class‘ is a keyword and declares that a new class definition follows. ‘First‘ is a Java identifier that specifies the name of the class to be defined.

Every class definition in Java begins with an opening brace” {“and ends with a matching closing brace”}”, appearing in the last line of the Java program. It is similar to a C++ class construct.

The third line:
public static void main(String args[])
defines a method named main. It is similar to the main() function of C/C++. Every Java application program must include the main() method. This is the starting point for the interpreter to begin the execution of the program. A Java application can have any number of classes but only one of them must include a main method to initiate the execution. This line contains a number of keywords such as static, public, and void.

The static means the method will be called without instantiating the class i.e. to execute the main method there is no need to create the object of First class. The public specifies that the method will be called outside the class, in this case the main method is called from within the operating system. The void means that the method does not return any value.

Summary of major points so far

  • Program execution begins from ‘class First
  • Keywords are written in lower-case
  • Statements are terminated with a semi-colon
  • Text strings are enclosed in double quotes
  • Java is case sensitive, use lower-case and try not to capitalize variable names
  • print(), println() can be used to display text to the screen in the next line
  • The curly braces {} define the beginning and end of a program block
  • The keyword public is an access modifier that declares the main method as unprotected and therefore making it accessible to all other classes. It is similar to the C++ public modifier.

Leave a Reply