Anatomy of a Java Program
Explore the essential structure of a Java program by understanding how to define a class, implement the standard main method, and write executable statements. This lesson helps you grasp the foundational template required to build any Java application, including how Java handles program execution and output.
We'll cover the following...
Java requires a specific structure before we can write a single line of logic. Unlike some scripting languages where you can simply type a command and run it, Java demands a formal hierarchy. A Java source file can contain one or more type declarations (classes, interfaces, enums, records). Classes can contain fields and methods, and methods contain executable statements.
In this lesson, we will build a complete runnable program, establishing the template we will use for every application in this course.
Understanding the class declaration
In Java, all code must live inside a class. A class acts as a container for our application's logic. When the Java Virtual Machine (JVM) runs a program, it looks for a class to load.
We will study how classes work internally later. At this stage, the goal is simply to recognize the outer structure.
Two important rules for now:
The class name must match the filename exactly, including capitalization. If we define a class named
Welcome, we must save it in a file namedWelcome.java. If these names do not match, the compiler will reject our code.A Java program needs at least one class containing a valid
mainmethod for the JVM to start running code.
Here is the most basic valid Java file. It defines a container but contains no logic yet.
Line 1:
public class Welcomedeclares a new class namedWelcome. The opening brace{marks the beginning of the class body.Lines 1–3: The outer braces
{and}define the boundary of the class body. Inside those braces, we place one or more methods.
Understanding the main method
A class defines a container, but it does not automatically do anything. To run code, the JVM needs a specific entry point; a place to start execution. This entry point is a method named main.
When we run a Java program, the JVM searches for a method with a very specific signature. We place this method inside our class definition.
Line 2: The
public static void main(String[] args)defines the method.String[] args: This parameter is an array of strings. It is designed to capture command-line arguments and text values passed to the program when it is launched. Even if our simple program does not use these inputs, the JVM requires this parameter to be present in the signature.
Lines 2–4: Inside the curly braces
{}, we place the statements that the JVM executes sequentially from top to bottom.
In standard Java, the launcher expects public static void main(String[] args). Newer Java versions have preview features that relax some of these rules, but we will stick to the standard signature in this course.
Note: You will see keywords like public and static in method definitions frequently. These control how other parts of the system access our code. We will explain exactly what they mean when we study classes and access control later. For now, treat them as required boilerplate for the main method.
Writing statements and output
Now that we have a class with an entry point in it, we can write instructions or statements for the computer to execute. In Java, we terminate every statement with a semicolon (;). If we forget the semicolon, the code will fail to compile.
To see our program work, let’s send text to the console. Java provides two primary commands for this: print and println.
System.out.println(): Prints the text and then moves the cursor to the next line. Subsequent output will appear below it.System.out.print(): Prints the text but keeps the cursor on the same line. Subsequent output will appear immediately to the right.
Here is our complete, runnable program demonstrating both.
Line 3: The
System.out.print("Loading...");prints “Loading...” but waits on the same line.Line 4: The
System.out.println("Done!");prints “Done!” immediately after “Loading...”, resulting in “Loading...Done!” on a single line, and then moves the cursor down.Line 5: The
System.out.println("Welcome to Java!");prints the text and moves to a new line.
A note on imports
Real-world programs rarely work in isolation. We often need to use pre-written code provided by the Java Class Library, such as tools for generating random numbers, handling dates, or reading files.
To use these tools, we must import them. The import statement tells the Java compiler where to find a specific class that is not defined in our current file. These statements must appear at the very top of the file, before the class definition.
We do not need imports for basic text output because System is part of the java.lang package, which Java imports for us automatically. However, as our programs grow, we will add imports here.
At line 1, the import java.util.Random; makes the Random utility class available for us to use later in the file.
We have now established the fundamental anatomy of a Java program. We create a file, define a class with a matching name, insert a main method as our entry point, and write sequential statements ending in semicolons. This structure (File, Class, Method, Statement) is the blueprint we will use for every application we build, from simple exercises to complex systems.