Sub topic 2
Anatomy of a Java Program
Every Java program is made of classes. A class is a container for code: it holds data and the instructions that work with that data. In this chapter, we look at the parts of a Java file, so that when you write your first program in the next chapter, the shape of the code will feel familiar.
The Class Is the Unit
Java does not let code float around freely. Every line of executable code lives inside a class, and each class lives in a file whose name matches the class name. A class called Main must be stored in a file named Main.java.
Here is the smallest legal Java program:
public class Main { public static void main(String[] args) { System.out.println(); } }Let's take it apart piece by piece.
Parts of the Smallest Program
public class Main— this declares a class namedMain. The wordpublicmeans other code is allowed to see and use this class. For now, treatpublic classas a fixed opening phrase.{ ... }— curly braces mark a block, a region of code that belongs together. Everything between{and}is part of the class.public static void main(String[] args)— this is the main method, the entry point. When you run a Java program, the computer looks for a method with exactly this signature and starts there.voidmeans the method returns no value.String[] argsis a place to receive text typed on the command line when the program starts.System.out.println();— a statement, one instruction. This one prints a blank line to the screen. Statements end with a semicolon (;).
You will write public static void main(String[] args) many times. At this stage, you do not need to understand each word; you need to recognize it as "the door into the program."
A Visual Map
Rules of the Shape
Three rules cover almost every file you will write:
- The file name must match the public class name:
Main.javacontainspublic class Main. - Braces come in pairs. If you open a
{, you must close it with a}. Misplaced braces are the most common beginner error. - Statements end with a semicolon. If you forget it, the compiler will point at the next line and complain.
What Goes Inside
A class can contain more than one method. Later chapters will add fields (data stored in the class) and more methods. The structure always looks the same: class on the outside, methods inside it, statements inside the methods.
public class Main {
public static void main(String[] args) {
System.out.println("Inside main");
}
}This program prints Inside main when run. That is the whole story of program structure: a file, one public class, a main method, and statements that do the work.
Exercise
What happens if you name the file Hello.java but the class inside is called Main?
Show answer
The compiler complains. The public class name and the file name must match, so the file should be Main.java.
Exercise
What is the main method's job in a program?
Show answer
It is the entry point. When the program runs, execution starts at the first statement inside main.
Exercise
Which symbol ends a Java statement?
Show answer
The semicolon: ;