This article is designed for beginners. We’ll go over the basic structure of a Java program in a simple, easy-to-understand way. Soon, we’ll move on to Object-Oriented Programming (OOP).
When writing a Java program, it always follows a certain structure. Think of it like a small factory: it takes in raw materials (input data), processes them (performs calculations), and produces a finished product (output).
1. Classes – The Foundation of a Java Program
In Java, everything revolves around classes. A class is like a container that holds the program’s code. In simple programs, there’s usually just one class, and its name matches the filename (MyProgram.java
).
public class MyProgram {
// Program code goes here
}
However, in larger applications, there can be multiple classes across different files. In that case, the main class doesn’t necessarily have to match the program’s name. What matters most is that one of the classes contains the main
method—this is where execution starts.
2. The main
Method – The Program’s Entry Point
Every Java program starts with the main
method. This is where execution begins.
public class MyProgram {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
public static void main(String[] args)
— This is the required method signature that Java looks for when running a program.System.out.println("Hello, world!");
— This prints text to the console.
3. Variables – Storing Data
Programs need to store and manipulate data, which is done using variables. A variable holds different types of data, such as numbers and text.
public class MyProgram {
public static void main(String[] args) {
int age = 30; // Whole number
double price = 19.99; // Decimal number
String name = "Anna"; // Text (string)
System.out.println("Name: " + name + ", Age: " + age);
}
}
4. Operators and Logic – Controlling the Flow
Java provides arithmetic operators (+
, -
, *
, /
) for calculations, as well as conditions and loops to control program flow.
Example of a Conditional Statement (if-else):
public class MyProgram {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("Access granted");
} else {
System.out.println("Access denied");
}
}
}
Example of a Loop (for):
public class MyProgram {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Step " + i);
}
}
}
This loop runs five times, printing each step.
5. Methods – Reusing Code
To avoid repeating code, we use methods. A method is like a mini-program inside a program—it performs a specific task when called.
public class MyProgram {
public static void main(String[] args) {
sayHello("Anna");
sayHello("Ivan");
}
public static void sayHello(String name) {
System.out.println("Hello, " + name + "!");
}
}
Here, the sayHello
method takes a name as an argument and prints a greeting.
6. Visualizing the Program Structure
A simple Java program is structured like this:
📂 Project (MyProgram)
│
├── 📄 MyProgram.java <-- Main class with the main() method
│
└── 📄 Other files (if any) <-- Additional classes
Here’s how the execution flow works:
[ Program Start ]
↓
[ Class MyProgram ]
↓
[ main() Method ] <-- Entry Point
↓
[ Code Execution ]
↓
[ Program End ]
When a Java program starts, it looks for the main()
method, runs its code, and then exits. As programs grow, they may include multiple classes and files, but the fundamental structure remains the same.
7. Summary of Java Program Structure
- Class – The main container for code.
main
Method – The program’s entry point.- Variables – Store data.
- Operators, Conditions, Loops – Control logic and flow.
- Methods – Help reuse code efficiently.
A Java program is like a well-organized system where classes, variables, methods, and conditions work together to produce the desired outcome.
Here is a simple Java program that demonstrates the key concepts covered in the article. Each part of the code includes comments explaining its purpose.
// This is a basic Java program demonstrating program structure, variables, methods, and execution flow.
public class MyProgram { // The main class (container for code)
// The main() method - this is where execution starts
public static void main(String[] args) {
System.out.println("Program started...");
// Declaring and initializing variables
int age = 25; // Integer variable
double price = 19.99; // Decimal number
String name = "Alice"; // String (text)
// Printing variable values
System.out.println("Name: " + name + ", Age: " + age);
System.out.println("Product Price: $" + price);
// Calling methods to perform actions
sayHello(name);
int sum = calculateSum(10, 20);
System.out.println("Sum of numbers: " + sum);
System.out.println("Program ended.");
}
// A method to print a greeting message
public static void sayHello(String name) {
System.out.println("Hello, " + name + "!");
}
// A method to calculate the sum of two numbers
public static int calculateSum(int a, int b) {
return a + b;
}
}
What This Code Demonstrates:
- Class (
MyProgram
) – The container for all code. main()
Method – The entry point of the program.- Variables (
int
,double
,String
) – Used to store and manipulate data. - Printing Output (
System.out.println()
) – Displays messages and variable values. - Methods (
sayHello()
andcalculateSum()
) – Show how to organize reusable code. - Execution Flow – The program runs step by step, calling different parts of the code.
This simple program clearly reflects the structure and execution process of a Java application.
Soon, we’ll move on to Object-Oriented Programming (OOP) and learn how to build more complex applications.