
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Basic Structure of a Java Program
In this article, we will learn about the basic structure of a program in Java. Java is widely used for developing large-scale applications, including Android apps, web applications, and enterprise software. To write a Java program, it's essential to understand its basic structure, which consists of several key components.
Components
A typical structure of a Java program contains the following elements:
- Package declaration
- Import statements
- Comments
- Class definition
- Class variables, Local variables
- Methods/Behaviors
- Main Method

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.
Syntax
The following is the syntax:
Package Directory_name;
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.
Syntax
The following is the syntax:
import Package_name;
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.
Syntax
The following is the syntax:
// use double backslahses to comment in Java
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.
Syntax
The following is the syntax:
public class Class_Name { //Class declaration }
Variables
The variables store 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.
Syntax
The following is the syntax:
int variable_name = 5 ; // variable declaration
Methods/Behaviors
A set of instructions that 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.
Main Method
Execution of a Java application starts from the main method. In other words, it's an entry point for the class or program that starts in Java Run-time.
Syntax
The following is the syntax:
public static void main(String args[]) { // Main Method }
Example of Basic Structure of a Java Program
Below is an example of a simple calculator in Java basic structure:
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 PointFrom10 Apr 2025 07:32:33 GMT Welcome to Tutorials PointFrom10 Apr 2025 07:32:33 GMT Welcome to Tutorials PointFrom10 Apr 2025 07:32:33 GMT Welcome to Tutorials PointFrom10 Apr 2025 07:32:33 GMT
Example for Simple Calculator in Basic Structure
Below is an example of a simple calculator in Java basic structure:
package com.example; // Package declaration import java.util.*; // importing utility package public class SimpleCalculator { // Class definition private static final String APP_NAME = "Simple Calculator"; // Class constant (unchanging value) public static void main(String[] args) { // Main method double num1 = 5.5; // Pre-assigned variables double num2 = 3.2; System.out.println("Welcome to " + APP_NAME); System.out.println("First number: " + num1); System.out.println("Second number: " + num2); double sum = addNumbers(num1, num2); System.out.println("The sum is: " + sum); } public static double addNumbers(double a, double b) { // Method to add two numbers return a + b; } }
Output
Welcome to Simple Calculator First number: 5.5 Second number: 3.2 The sum is: 8.7