Chapter 03 of 57

Java Program Structure

When you look at a Java program for the first time, it can seem a little confusing. You see classes, methods, curly braces, parentheses, and other symbols all mixed together. The good news is that Java programs follow a clear structure. Once you understand that structure, reading and writing Java code becomes much easier.

Let's take a simple Java program and understand its basic structure.

class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

There are several parts in this small program, so let's understand them one by one.

Class

A Java program is generally organized using classes. In our example, we have:

class Main {
    
}

Here, class is a Java keyword used to create a class, and Main is the name of our class.

You can think of a class as a container that holds related code. Our methods, variables, and other parts of the program can be placed inside the class.

For now, you don't need to worry too much about what a class actually represents. We will study classes and objects in detail later.

Main Method

Inside the class, we have:

public static void main(String[] args) {

}

This is the main method. When we run a Java application, execution starts from this method.

For example, if we write:

class Main {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Java starts executing the program from the main() method and then executes the statement inside it.

You will notice several words such as public, static, and void in the main method. Don't worry about them yet. Each of these has a specific meaning, and we will learn them separately.

For now, remember:

The main() method is the starting point of a Java program.

Statements

Inside the main() method, we can write statements that tell Java what to do.

For example:

System.out.println("Hello World!");

This is a statement that prints text to the console.

We can have multiple statements:

public static void main(String[] args) {
    System.out.println("Hello");
    System.out.println("Welcome");
    System.out.println("Java");
}

Java executes these statements one after another.

Most Java statements end with a semicolon ;.

Curly Braces

You will see curly braces { } everywhere in Java.

They are used to define a block of code.

For example:

class Main {
    
}

The braces show where the Main class begins and ends.

Similarly:

public static void main(String[] args) {
    
}

The braces show where the main() method begins and ends.

As your programs become larger, you will have classes, methods, loops, and conditional statements, and many of them will use curly braces to group their code.

Comments

Java also allows us to write comments in our programs. Comments are notes written for humans and are ignored when the program runs.

A single-line comment starts with //:

// This program prints a message
System.out.println("Hello World!");

We can also write a comment across multiple lines using /* */:

/*
   This is a
   multi-line comment
*/
System.out.println("Hello World!");

Comments are useful when you want to explain what a particular part of your code is doing.

A More Complete Example

Now let's put these parts together:

// Our first Java program
class Main {

    public static void main(String[] args) {

        System.out.println("Welcome to Java!");
        System.out.println("Let's learn programming.");

    }
}

Here, the Main class contains the main() method, and the main() method contains the statements that we want Java to execute. The comments are simply notes for the programmer and do not affect the output.

The output will be:

Welcome to Java!
Let's learn programming.

Do We Always Need a main() Method?

For the simple Java applications we are learning, the main() method is normally where execution begins. However, not every Java file you encounter will necessarily be a standalone program with its own main() method. Java is also used to create classes, libraries, frameworks, and other components that are used by other parts of an application.

For now, when you are writing simple Java programs and want to run them directly, you will commonly use the main() method.

Quick Revision

A basic Java program is organized into classes, and the main() method is commonly used as the starting point of a standalone Java application. Statements inside the method tell Java what to do, and most statements end with a semicolon. Curly braces are used to define blocks of code, while comments allow us to write notes that are ignored during execution.

A simple way to remember the structure is:

Class
  ↓
Main Method
  ↓
Statements
  ↓
Output

Once this structure becomes familiar, Java programs will stop looking complicated. As we continue, we'll learn what each part actually does and how to use it properly.