Explain the basic structure of a program in Java?


A typical structure of a Java program contains the following elements

  • Package declaration
  • Import statements
  • Comments
  • Class definition
  • Class variables, Local variables
  • Methods/Behaviors

Package declaration

A class in Java can be placed in different directories/packages based on the module they are used. For all the classes that belong to a single parent source directory, a path from source directory is considered as package declaration.

Import statements

There can be classes written in other folders/packages of our working java project and also there are many classes written by individuals, companies, etc which can be useful in our program. To use them in a class, we need to import the class that we intend to use. Many classes can be imported in a single program and hence multiple import statements can be written.

Comments

The comments in Java can be used to provide information about the variable, method, class or any other statement. It can also be used to hide the program code for a specific time.

Class Definition

A name should be given to a class in a java file. This name is used while creating an object of a class, in other classes/programs.

Variables

The Variables are storing the values of parameters that are required during the execution of the program. Variables declared with modifiers have different scopes, which define the life of a variable.

Main Method

Execution of a Java application starts from the main method. In other words, its an entry point for the class or program that starts in Java Run-time.

Methods/Behaviors

A set of instructions which form a purposeful functionality that can be required to run multiple times during the execution of a program. To not repeat the same set of instructions when the same functionality is required, the instructions are enclosed in a method. A method’s behavior can be exploited by passing variable values to a method.

Example

Live Demo

package abc; // A package declaration
import java.util.*; // declaration of an import statement
   // This is a sample program to understnd basic structure of Java (Comment Section)
   public class JavaProgramStructureTest { // class name
      int repeat = 4; // global variable
      public static void main(String args[]) { // main method
      JavaProgramStructureTest test = new JavaProgramStructureTest();
         test.printMessage("Welcome to Tutorials Point");
   }
   public void printMessage(String msg) { // method
      Date date = new Date(); // variable local to method
      for(int index = 0; index < repeat; index++) { // Here index - variable local to for loop
         System.out.println(msg + "From" + date.toGMTString());
      }
   }
}

Output

Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT
Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT
Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT
Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT

Updated on: 07-Feb-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements