Search

Structure of Java Code

Notice
The Java posts on this page are based on what I learned from the book Jump to Java, interpreted and organized in my own way. If you find any mistakes or inaccuracies, please feel free to let me know, I’ll review and correct them as soon as possible.

Example of Java Code

Java is a representative object-oriented programming (OOP) language, and every Java program is structured around a class.
Each Java program consists of one or more classes, and within each class, there are methods and statements that define what the program does.
In other words, Java code follows a hierarchical structure.
ClassMethodStatement
Understanding this hierarchy allows you to see clearly how a Java program is built and executed.
Let’s take a closer look at each layer in detail.

1. Class Block

In Java, a class is the fundamental unit of a program and serves as a blueprint for objects.
A single Java file typically contains one class, and the class name should match the file name.
public class HelloWorld { // <- The class HelloWorld starts from here // Field (member variable) String message = "Hello, Java!"; // Method (behavior) public void printMessage() { System.out.println(message); } } // <- The class HelloWorld ends here
Java
복사
public class HelloWorld is the class declaration.
public – an access modifier that makes the class accessible from anywhere.
class – the keyword used to define a class.
HelloWorld – the class name, which typically starts with a capital letter.
Everything inside the curly braces {} belongs to that class, including its fields and methods.
The class block forms the outer structure of a program. Inside it, we define method blocks, which contain the executable logic.

2. Method Block

Inside a class, you can define multiple methods. A method is a group of statements that perform a specific action, in other words, it represents the behavior of the program.
Among all methods, the most important one is the main() method.
Every Java program begins execution from the main() method.
That’s because the JVM (Java Virtual Machine) always looks for and runs this method first.
public class HelloWorld { public static void main(String[] args) { // <- The main method starts from here System.out.println("Hello, Java!"); } // <- the main method ends here }
Java
복사
public static void main(String[] args)
public – means this method can be accessed from anywhere.
static – allows the method to run without creating an instance of the class.
void – indicates that the method doesn’t return a value.
String[] args – an array that holds command-line arguments passed when the program starts.
The curly braces {} that follow define the method block, and inside it are the statements that actually execute.
Simply put, a method is the smallest unit of executable logic in a Java program.

3. Statements

A statement is a single instruction that performs an action. Every statement must end with a semicolon (;), which tells the Java compiler that the command has ended.
int number = 10; // Variable declaration System.out.println(number); // Output statement number = number + 5; // Arithmetic operation
Java
복사
Variable declaration : defines a space in memory to store data
int number = 10;
Output statement : prints a message or value to the console
System.out.println(number);
Operation statement : changes or calculates the value of a variable
number = number + 5;
These statements together form a method, and multiple methods together form a class.

Complete Example

public class HelloJava { // Class block begins // Method block: entry point of the program public static void main(String[] args) { // Statement 1: variable declaration String greeting = "Hello, Java!"; // Statement 2: output System.out.println(greeting); } // Additional method public void sayGoodbye() { System.out.println("Goodbye, Java!"); } // Class block ends }
Java
복사
Structure analysis
Class Block : The entire HelloJava class
Method Blocks : main() and sayGoodbye() methods
Statements : The actual lines executed inside each method
As you can see, Java’s structure is strictly organized: every piece of code must reside within a class, every executable action inside a method, and every action line is a statement.
Key takeaways
All Java code must exist inside a class.
Program execution always starts from the main() method.
The semicolon (;) is crucial, it marks the end of each statement.