Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Structure of a Java Program | Getting Started
Introduction to Java

Structure of a Java Program

Swipe to show menu

You'll see how a simple Java program is structured and what its main parts are.

Here is an example of a Java program:

Main.java

Main.java

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println("Hello, world!"); } }

This may look a bit overwhelming at first, but don't worry — we'll break it down step by step.

At the highest level, every Java program is written inside a class. A class is simply a container that holds your code. In this example, the class is called Main.

Inside the class, you have the main method:

public static void main(String[] args)

This is the starting point of every Java program. When you run your code, Java always begins execution from this method.

Inside the main method, you have the actual instruction:

System.out.println("Hello, world!");

This line tells Java to display text on the screen. In this case, it prints:

Hello, world!

You will also notice curly braces:

{ }

These are used to group code together. Everything inside a pair of curly braces belongs to the same block.

Finally, you'll see a semicolon at the end of most statements:

;

A semicolon marks the end of an instruction. You can think of it like a period in a sentence — it tells Java that the command is finished.

question mark

What is the role of the main method in Java?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Structure of a Java Program

You'll see how a simple Java program is structured and what its main parts are.

Here is an example of a Java program:

Main.java

Main.java

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println("Hello, world!"); } }

This may look a bit overwhelming at first, but don't worry — we'll break it down step by step.

At the highest level, every Java program is written inside a class. A class is simply a container that holds your code. In this example, the class is called Main.

Inside the class, you have the main method:

public static void main(String[] args)

This is the starting point of every Java program. When you run your code, Java always begins execution from this method.

Inside the main method, you have the actual instruction:

System.out.println("Hello, world!");

This line tells Java to display text on the screen. In this case, it prints:

Hello, world!

You will also notice curly braces:

{ }

These are used to group code together. Everything inside a pair of curly braces belongs to the same block.

Finally, you'll see a semicolon at the end of most statements:

;

A semicolon marks the end of an instruction. You can think of it like a period in a sentence — it tells Java that the command is finished.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
some-alt