Basic Structure of Java Program

This tutorial explains the basic structure of java program. It covers a brief detail of common components of a java program and the order in which they should be declare/defined inside a java program. Writing the program in a structured way helps you and your team mate to understand the program easily.

Some of the orders of components are mandatory to follow while others are not, but it's a good practice to follow the orders given in this tutorial as it makes your program more readable. The basic components of a java program and their order of declaration is :

  • Package Statement
  • Import Statement
  • Documentation or comment
  • Class Definition
    • Declare instance and static variables
    • Declare constructors(if any)
    • Main method or other methods

Before we start to understand these components, let's see a program demonstrating above structure first, then we will see each component one by one.

Class demonstrating basic structure of java program.

 package mypack;
 
 import java.util.Date;
 
 /**
  * The ProgramStructure class used to demonstrate 
  * the structure of java program.
  *
  * @author  refreshjava
  */
 class ProgramStructure  {
    /* The variable date is used to store 
     the current date. */
    public static Date date = new Date();
    // This variable stores website name. 
    private String siteName = "refreshjava.com";

    /**
     * The no argument constructor.
     */
    public void ProgramStructure() {
       System.out.println("No argument constructor");
     }

    /**
     * This method prints the string passed to it in console. 
     *
     * @param  str  The string to be printed
     */
    public void printMessage(String str) {
       System.out.println(str);
     } 
          
    /**
     * Entry point, it's the first method to be called  
     * while running this class.
     */
    public static void main(String args []) {
       ProgramStructure obj = new ProgramStructure();
       obj.printMessage("Java program of code structure");
       System.out.println("Program was written on = "+date);
       System.out.println("Program author : "+obj.siteName);
     }       
 }        

Output:

Java program of code structure
Program was written on = Tue Apr 28 09:04:36 IST 2020
Program author : refreshjava.com

To run this program you must have knowledge about packages in java, if not refer packages in java tutorial first.

Package Statement in Java

A package is a group of similar types of classes and interfaces. A package name is basically a directory in your computer where the given class/program is saved. It's an optional statement, if you don't declare any package, your program will be saved inside a default package(no name package). Package declaration in above program is :

 package mypack;  

There can be only one package statement inside a program and it must be the first line of code in your program. The program must be saved inside that package(directory), for example the above program must be saved inside mypack directory. To get more detail about packages refer packages in java tutorial.

Import Statements in Java

If your program wants to access classes of any other packages, then those classes or packages must be imported in your program first. Import statements basically imports/includes the classes of a packages in your program, once imported you can use the classes of that packages in your program.

All the Import statements must be written after package name declaration and before class name declaration. We can import a single class or all classes of a package like below :

 import java.util.Date; //imports only Date class
 import java.util.*;  //imports all the classes of java.util package

To get more detail about import statements, refer package import in java tutorial.

Documentation or comment in Java

Documentation or comment is basically a little detail about your components(class, method, variable etc) like what it does or what it is used for etc. Documentation makes it easy for you and your team mates to understand your program easily. Documentation or comment about a component should be written just before it's declaration.

Though it's optional to write the documentation or comment but it's a good practice to write them at the time of designing the component. It's not necessary to write the documentation for each and every element of a program. Java provides the following approaches to write documentation or comments in your programs.

  • Single line comment : It starts with two forward slashes // and followed by your comment. Any text after // and till the end of line is considered as comment. You can use single line comments anywhere and up to any number of times in your program.
  •  // This is single line comment
    
  • Multi line comment : It starts with /* and ends with */. Any text between /* and */ will be considered as comment. You can use multi line comments anywhere and up to any number of times in your program.
  •  /* 
        This is multi line comment
        This is multi line comment
      */
  • Documentation : It starts with /** and ends with */. Any text between /** and */ will be considered as document. We generally use documentation for classes, constructors and methods.
  •  /** 
      * This is documentation, used to give 
      * the little detail of component.
      */

Does these documents or comments executed while compiling or executing the program ?

No, Java completely ignores them, which means they are not executed while compiling or executing the program.

Note : While learning java, you can avoid to use comments as it takes time but if you are working on any project or application then you should follow to write the documents or comments about different components of your program as it will make your program more readable.

Class Definition in Java

In java, a program is basically a group of one or more classes. We define a class using class keyword, followed by class name. As a good practice programmer should write a meaningful name to their classes. Everything given inside { } after class name is the part of that class.

As a good practice you should write little detail about your class before it's declaration, as given in above program. To get more detail about classes refer classes in java tutorial.

Declare static and non static variables

Inside the class, first you should declare all your static and non static variables of that class. Though it's not mandate, but it's a good practice to define the variables before defining methods or constructors of a class. It makes easy to understand your program. To get details about variables refer variables in java tutorial.

Declare constructors

After variables you should define the constructors(if any) of that class, again it's not mandatory, it's just a good practice to follow. You should define all your constructors before defining the methods. Also write little detail about the constructors as given in above example to make it easy to understand that constructor. To get detail about constructors refer constructors in java tutorial.

Declare main method or other methods

After defining constructors, you should define the main method and other methods of that class. You should also write little detail about the method before it's declaration to make it easy to understand. In java, to run a program independently it must have a main method like below :

 public static void main(String args []) {
    // method body
  }  

The main method is the entry point of a program which means it's the fist method which will be called while running your program. To get detail about methods refer methods in java tutorial.

What if I need to declare an interface in my program ?

Generally interfaces are declared in a separate file, but if needed you can declare it before class declaration as interfaces are designed to be implemented by classes. Though it's not mandatory to declare before class but doing so makes your program more readable.

★★★
  • Keep in mind that java keywords and code is case sensitive.
  • Generally You need to define a class to write a program in java.
  • You must save your program with same name as class name having main method.
  • A program may have only variables, methods or constructors.
  • Generally each class in java is defined in separate file but we can define multiple classes in same file.