In this tutorial you will learn about the Java Program Structure and its application with practical example.
Java Program Structure
In Java, like any other programming language, we have a specification available for program structure, every java program consists of the following building blocks –
Documentation Section:- Documentation section is generally meant to include a set of comments, that is used to provide information about the program written as the name of a program, the utility of the program, date of creation, date of last modification, author name, licensing or copyrights information and any other information that programmer wish to put for the references.
Example:
1 2 3 4 5 6 7 |
//Program Name: First Program /* Version: 1.0 Description: Java program basic program structure. Author: @noname Date Created:21-02-2018 */ |
Package Statement:- This is the section used for package declaration, the package statement declares a package name and notifies the compiler that the classes defined here will belong to this package.
Example:-
1 |
package package_name; |
Import Statements:- The import statements are used to import packages that contain all of the required classes, and interfaces. java.lang.*; the package is imported by default and this package is known as the default package.
Example:-
1 |
import employee.test; |
Interface Statement:- An Interface is like a class that usually contains a group of method declarations. It’s an optional section and can be used when we want to implement multiple inheritances within a program.
Class Definition:- The classes are used to map real-world entities into programming. The Classes are the key building block of any Java program. A java program may include several class definitions.
Main Method Class:- A java program may include multiple classes, but there can be only one class that will define the main method.
Main Method Definition:- This is the most vital part of each and every java program, it is mandatory for the java program to have a class with a primary method definition. There can be only one class that will define the primary method. The Java program execution starts with the main method. Java program can not be executed without the main method. The main method is responsible for the execution of all the user-defined statements, functions, and library functions. The main method is further structured into – variable declaration, function declaration, and user-defined executable statements.