Structure of Java Program

Java program is having below sections.

 

·       Package declaration

·       Import statements

·       Comments

·       Class definition

·       Class variables, Local variables

·       Main Method

·       Methods/Behaviors

 

Package declaration: A class in Java can be placed in different directories/packages based on the module they are used. 

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 

Comments: The comments in Java can be used to provide information about the variable, method, class or any other statement 

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

Class Variables, Local Variables: The Variables are storing the values of parameters that are required during the execution of the program

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.

  

Example:

 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());

      }

   }

}


Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?