Table of Contents

Program Structure

Using the Java programming language, we can develop a wide variety of applications. So, before diving in-depth, it is necessary to understand the basic structure of the java program.

A typical structure of a Java program contains the following elements:

  • Documentation Section
  • Package Declaration
  • Import Statements
  • Interface Section
  • Class Definition
  • Class Variables and Constants
  • Main Method Class
  • Methods and Behaviours

1.Documentation Section

The documentation section is important but optional for a Java program. It includes basic information about a Java program. The information includes the author's name, date of creation, version, program name, company name, and description of the program.

/* Author: James Gosling Java Programming Developed in 1991 Sun Microsystems */

2.Package Declaration

There can be only one package statement in a Java program. It must be defined before any class and interface declaration.

``` java package student;//student is the name of the package ```

3. Import Statements

The import statement represents the class stored in the other package. We use the import keyword to import the class. It is written before the class declaration and after the package statement.

import java.util.Scanner; //it imports the Scanner class only import java.util.*; //it imports all the class of the java.util package

4. Interface Section

We use the interface keyword to create an interface. An interface is slightly different from the class. It contains only constants and method declarations.

interface car { void start(); void stop(); }

5. Class Definition

In this section, we define the class. It is a vital part of a Java program. Without the class, we cannot create any Java program. We use the class keyword to define the class. The class is a blueprint of a Java program.

class Student //class definition { }

6. Class Variables and Constants

In this section, we define variables and constants that are to be used later in the program. In a Java program, the variables and constants are defined just after the class definition. The variables and constants store the values of the parameters.

class Student //class definition { String sname; //variable int id; }

7. Main Method Class

In this section, we define the main() method. It is essential for all Java programs. Because the execution of all Java programs starts from the main() method. In other words, it is an entry point of the class. It must be inside the class. Inside the main method, we create objects and call the methods.

public class Student //class definition { public static void main(String args[]) { //statements } }

8. Methods and behaviour

In this section, we define the functionality of the program by using the methods. The methods are the set of instructions that we want to perform.

public class Greeting //class definition { public static void main(String args[]) { void display() { System.out.println("Welcome to Shape AI"); } //statements } }