How to Understand the Structure of Java Program

When we start our development journey, our focus is on which language we should prefer for development because every language has a standard structure for writing the program. Similarly, Java also has a standard structure. Java is an object-oriented programming language, and this language is extremely popular among other languages because it is platform-independent and secure programming. In Java, we can develop a variety of applications, such as web, windows, and mobile applications. In this article, we will understand the basic structure of Java program in detail. After reading this article you will be able to develop your first Hello world Java program.

To write a program in Java, we first need to know all components of Java structure.

In a Java program, there are multiple components involved. Let’s discuss each component in detail.

Structure of Java Program

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

Documentation Section

The documentation section is especially important for any program but not mandatory. The document contains basic information about any program, like the Author name, date of creation or modification, version, program name, company name, and description of the program. This will help us to improve program reliability. The Java compiler will not consider this document during the compilation of the program. We use comments to write a statement in the document. We can write comments on a single line, or multi-lines based on program requirements.

Comment improves the documentation for the code, and it’s good practice for a developer.

Single line Comment:

We can use // before any statement or code line that is a single-line comment.

// Single-line comment

Multi-line Comment:

It starts with symbol /* and ends with */. We can write anything between these two symbols.

/*This is an example of
multiline comment*/

Documentation Comment:

It is a comment that starts with symbol /** and ends with */

/**This is a document comment type*/

You can also read :

Magic Number Program in Java with Examples

How to write hello program in Java

What is a strong number in Java and how to code it?

Package Declaration

The package declaration is an option of Structure of Java Program. It is located immediately following the documentation section. Specify the package name that contains the class. You can declare only one package statement in a Java program, and it must be placed before any class or interface declaration. It is important because this declaration helps organize classes into different packages and directories based on the module they are used. Keyword Package will be used to declare the package name. For example:

package javatpoint; // In this code, javapoint is a package name
package com.javatpoint; //here com is the root directory and javatpoint is the subdirectory

 

Import Statements

The packages have classes, interfaces, and enums, we can import them from these packages. Import keyword use to import the classes, this keyword represents classes stored in different packages. We can use Import keyword in different ways, import a specific class or all classes of package. Structure of Java program allow us to use multiple import statement. For Example:

import java.util.*; //This will import all the classes from util package
import java.util.StringTokenizer; // It import only the StringTokenizer class from util package

Interface Section

The Interface section is optional. This section allowed you to create an interface if you need. The interface keyword used to create an interface. An interface is unlike class there are slightly different from the class. This section accepts only declaration of any method not definition and constants. Most importantly, we cannot instantiate an Interface. If you want to get access of these interfaces in your class, then you must use keyword implements. An interface can extend another interface using the extends keyword. For example:

interface Laptop
{
void turnon();
void turnoff();
}

Class Definition

This section is most important Structure of Java Program, we cannot create any program without class. Each java program must be written under a class only, this is the most important principles of Oops (Object-oriented programming) that java follows. To define a class, we use keyword as class. Class contains the information like user defined methods, variables. For example:

class JavaProgram //class definition
{
}

Class Variables and Constants

Variables and constants are required for any program. Variables and constants can be defined after class definition. Variables and constants can contain uppercase, lowercase underscore( _ ) and dollar ($) but cannot starts with any digit (0-9) or not contain any special characters.

We can create multiple variables in same class. Variable also knows as identifier in Java. For example:

class JavaProgram  //class definition
{
//Syntax - <Data-Type> <Variable Name> = value
String ProgramName;
int id;
double percentage;
}

Main Method Class

This section is compulsory part of the structure of Java programJava compiler search for this Method in any program to start execution, in other word, it is an entry point for compiler to any program. Main method called/invoked by the JVM (Java Virtual Machine). For example:

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

Methods and behavior

Methods are collection of statement that perform a specific task. This statements execute at runtime and provide result. Method name typically starts with a lowercase letter. Method can be reused many times in your Java program. For example:

public class Demo //class definition
{
public static void main(String args[])
{
void displayMessage()
{
System.out.println("Welcome to TechieTrail.com");
}
//statements
}
}

Leave a Comment

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

Scroll to Top