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
12345678package 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.
Thanks for your feedback!
Ask AI
Ask AI
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
12345678package 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.
Thanks for your feedback!