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
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.
package example; // where example is name of package package root.example; // root is the parent directory of example
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.
import java.util.*; // It will import all classes of java.util package. import java.util.Scanner; // It will import only the Scanner class present in util 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-
class demo // demo is name of 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:
interface student // student is the name of interface { void add (); // add and fact are the methods declared inside it void fact (); }
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-
public void print () // “print” is name of method and public is access modifier. { System.out.println(“This is an Example”); }
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.
public class example // example is name of class. { public static void main ( String []args ) // Basic structure of main method { } }
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.
Import java.util.Scanner; public class example { public void add() { System.out.println(“Enter the first number ”); int a =sc.nextInt(); System.out.println(“Enter the second number ”); int b =sc.nextInt(); int c= a + b; System.out.println(“Sum of two numbers is ”+c); } public void multiply() { System.out.println(“Enter the first number ”); int x =sc.nextInt(); System.out.println(“Enter the second number ”); int y =sc.nextInt(); int z= x * y; System.out.println(“Multiplication of two numbers is ”+z); } public static void main (String []args) { Scanner sc=new Scanner(System.in); example obj=new example(); obj.add(); obj.multilply(); } }
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.