Before writing large Java applications, you need to understand how a Java program is organized. Java follows a structured approach in which classes, methods, variables, statements, and blocks work together to form an executable program.
At first, Java syntax can look strict because even a small program contains several keywords and symbols. The good news is that each part has a specific job. Once you understand the structure, reading and writing Java code becomes much easier.
A Basic Java Program
Let's start with a very small Java program:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Although this program contains only a few lines, it introduces several important building blocks of Java. Let's understand each part carefully.
1. Class Declaration
The first important part is the class declaration:
public class Main {
}
Here, class is a Java keyword used to define a class, while Main is the name of the class.
A class can be thought of as a container that holds related data and behavior. Java applications are built around classes, so understanding classes is fundamental to understanding Java.
The keyword public is an access modifier. It means the class can be accessed from other parts of the application, subject to Java's access rules.
2. Curly Braces
The curly braces { } define a block of code.
public class Main {
// Class body
}
Everything between the opening and closing braces belongs to the class. Java uses braces extensively to define classes, methods, loops, conditional blocks, and other structures.
Remember: Opening braces start a block and closing braces end it. Missing or incorrectly placed braces are common beginner errors.
3. The main() Method
Inside the class, our program contains the main() method:
public static void main(String[] args) {
}
For a traditional standalone Java application launched through the Java launcher, this method acts as the entry point. Program execution begins here.
The declaration contains several parts, and each one has a specific meaning.
| Part | Meaning |
|---|---|
| public | Allows the method to be accessible to the Java launcher. |
| static | Allows the method to be called without creating an object of the class. |
| void | Indicates that the method does not return a value. |
| main | The conventional name of the application entry-point method. |
| String[] args | An array of strings used to receive command-line arguments. |
4. Statements
A statement is an instruction that tells Java to perform an operation.
For example:
System.out.println("Hello, Java!");
This statement prints text to the console.
Many Java statements end with a semicolon ;. The semicolon tells the compiler where that statement ends.
Beginner tip: If Java reports an error near a line that looks correct, check the previous line too. A missing semicolon can cause the compiler to report an error somewhere that feels unrelated.
5. System.out.println()
The statement System.out.println() is commonly used to display information in the console.
System.out.println("Welcome to Java");
System.out.println(100);
The first statement prints text, while the second prints a number.
You will use console output frequently while learning Java because it is a simple way to see what your program is doing.
6. Variables
Variables are used to store values that a program needs to work with.
int age = 25;
String name = "Bibhu";
In the first line, int specifies the type of value, age is the variable name, and 25 is the assigned value.
In the second line, String represents text data.
7. Methods
A method is a named block of code that performs a particular operation.
public static void greet() {
System.out.println("Welcome!");
}
The method above is named greet. When the method is called, its statements can execute.
public class Main {
public static void greet() {
System.out.println("Welcome!");
}
public static void main(String[] args) {
greet();
}
}
Methods help break a large program into smaller, understandable pieces. Instead of putting every instruction inside one huge block, developers can separate responsibilities into meaningful methods.
8. Comments
Comments are notes written inside source code for developers. They are ignored by the compiler during normal compilation.
// This is a single-line comment
/*
This is a
multi-line comment
*/
Comments can be useful for explaining decisions, documenting unusual behavior, or temporarily disabling code during development. Good comments explain why something is done when the reason is not obvious from the code itself.
9. Package Declaration
Larger Java applications commonly organize classes into packages. A package declaration appears near the beginning of a Java source file.
package com.example.app;
public class Main {
}
Packages help organize related classes and reduce naming conflicts. They become especially important as applications grow beyond a handful of files.
10. Import Statements
Java provides a large standard library, but a class from another package may need to be imported before it can be referenced by its simple name.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
The import statement tells the compiler which type you want to refer to without writing its fully qualified name every time.
Typical Java File Structure
A Java source file can contain several common structural elements. Not every file needs every element, but the following order is useful to understand.
package com.example.app;
import java.util.Scanner;
public class Main {
// Variables
// Methods
public static void main(String[] args) {
// Statements
}
}
This structure gives you a useful mental map: package organization comes first, imports bring in required types, the class contains the program's members, and methods contain executable behavior.
A More Practical Example
Now let's combine several parts into one small program:
public class Student {
String name = "Rahul";
int marks = 85;
void displayStudent() {
System.out.println("Name: " + name);
System.out.println("Marks: " + marks);
}
public static void main(String[] args) {
Student student = new Student();
student.displayStudent();
}
}
The class is named Student. It contains two variables, name and marks, which represent student information.
The displayStudent() method prints that information. Inside main(), a Student object is created and the method is called.
This small example already demonstrates the relationship between a class, its data, its methods, and an object. These ideas form the foundation of Java's object-oriented programming model.
Java Naming Conventions
Java has widely followed naming conventions that make code easier to read and maintain.
| Element | Common Convention | Example |
|---|---|---|
| Class | PascalCase | StudentDetails |
| Method | camelCase | displayStudent() |
| Variable | camelCase | studentName |
| Constant | UPPER_SNAKE_CASE | MAX_MARKS |
| Package | Lowercase | com.example.app |
Naming conventions do not change how the program executes, but they make code significantly easier for other developers to understand.
Common Beginner Mistakes
- Forgetting the semicolon at the end of a statement.
- Using mismatched curly braces.
- Writing the class name differently from the public class declaration.
- Confusing a class with an object.
- Thinking every Java file must contain a main() method. A Java class can exist without being an application entry point.
- Using inconsistent naming conventions that make larger programs difficult to read.
Learning Checkpoint
Look at the following program and identify the class, method, variable, statement, and object:
public class Car {
String brand = "Toyota";
void showBrand() {
System.out.println(brand);
}
public static void main(String[] args) {
Car car = new Car();
car.showBrand();
}
}
If you can identify each part and explain its role, you are ready to move from basic program structure into the deeper concepts of Java classes and objects.
Interview Insight
A common interview question is: "What is the structure of a Java program?" A good answer should mention the package declaration when applicable, import statements, class declaration, fields or variables, methods, and executable statements. For a standalone application, you should also explain the role of the main() method.
Key idea: Java programs are structured, not random collections of statements. Classes organize related data and behavior, while methods organize executable operations.
Final Summary
A Java program is built from structured components such as packages, imports, classes, variables, methods, and statements. The class provides a container for related data and behavior, methods define operations, variables store values, and statements perform actions. In a traditional standalone application, the main() method provides the starting point for execution. Once you understand this structure, Java code stops looking like a collection of unfamiliar keywords and starts looking like a well-organized system where every part has a clear responsibility.
