Basic Structure of Java Program with Example

Java is a very popular language and used on 7 billion devices worldwide. It is one of the most secured, platform-independent, and object oriented programming languages that’s why it is necessary to be familiar with the basic structure of Java program. A typical Java program consists of the following sections:

  • Package statements
  • Documentation Section
  • Import statements
  • Class definition
  • Interface statements
  • Methods
  • Main method class

Basic Structure of Java Program

Basic Structure of Java Program

Image Source

Package Statements

It is optional to write in your Java program. Note that we can not use more than one package statement in our Java program. It should be mentioned before any Interface and class definition. package keyword is used for declaring the package name in the program.

Documentation Section

It is also an optional section to write but it is important to write for better readability and understanding. It contains basic information about the class, variables, methods, author’s name, program name, version, company name, date of creation, etc. The compiler does not execute of contents of the documentation section and it ignores the statements written in it. We use comments to write in it and comments can be single-line, multi-line, and documentation comments. For writing single-line comments we use (//) and for multi-line comments, we start with (/*) and end with (*/). Documentation comment starts with (/**) and end with (*/).

Example single-line comment:

// This example for writing single line comment.

Example multi-line comment:

/* It is an example for writing

Multi-line comment */

Example documentation comment:

/** It is an example for documentation comment */

Import Statements

It contains predefined classes, methods, interfaces. For using that predefined classes or interfaces in your program you will have to import that classes and with the help of “import” keyword, we can do that. Keyword “import” imports the classes defined in different packages.

It is written between classes declaration and package statements. With the help of the import keyword, we can import a specific class or all the classes of that package.

Class Definition

It is one of the most important sections of Java program and is necessary to write. Without a class, you can not create a Java program. At least one class must be present in your java program. You can create more than one class. For defining a class we use “class” keyword and after that, we write the name of the class you want to give. Given below is the example for defining a class-

Interface Statements

It is also an optional statement to write. If you want to use an interface then you can create it with the help of “interface” keyword. Interface contains methods declaration and constants. It does not contain the method definition and it can not be instantiated. Classes uses interfaces with the help of “implement” keyword and interface uses another interface with “extends” keyword. Given below is the example for creating an interface:

Methods

It is also an optional section. If you want use a method in your program for particular functionality then you can define it. Methods are created for performing a specific task and to avoid the rewriting of the same code. Methods can be created with the private, public, protected and default depends on your need and accessibility in your program. Given below is an example for creating a method-

Main Method Class

It is the most important section of your Java program. We define the main() method in this section and without this Java program would not be able to execute. Execution of Java program starts from main method. It is written inside a class and inside main() method we call methods and instantiate classes. Given below is the structure of main method and it must be written as it is.

Example Program:

Given below is an example of a complete Java program for the addition and multiplication of two numbers with the use of the above sections.

Various parts of the above program are described below:

Import java.util.Scanner

The statement is importing the Scanner class present in the util package. Scanner class is used for getting user input.

public class example

The above statement is written for the definition of class named “example” having “public” access modifier.

public void add()

It is a method defined for the addition of two numbers having “public” access modifier.

public void multiply()

It is another method defined for multiplication of two numbers having “public” access modifier.

public static void main (String []args)

It is main( ) method whenever compiler executes our program it always starts from main( ). The keyword static is used for calling the main( ) method without creating it’s object. The method is made public for accessing the main ( ) method from another package. Keyword “void” means it does not return any value. In arguments args[] array is passed of String data type and used for taking input parameter if your program run through console and  “public” is its access modifier.

System.out.println()

The line is used to print the text where System is predefined class and out is an object of printWriter class. The method println() is for printing the text as output on screen with new a line.

Leave a Comment

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