Lessons › coding and computer science · taught by Leo
Java basics: classes, main, and statements
Learn the structure of a basic Java program, where execution starts, and how braces and semicolons organize code.
Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.appJava runs in clear steps
Java is picky, but that helps. It wants each step written clearly, in the right order.
Think of it like a recipe. You write the ingredients, then the steps, and the computer follows them exactly.
A Java program has a recipe
A basic Java program usually has a class, a main method, and statements inside main.
Those three parts are the starter kit for AP CSA.
Predict the program part
Which part is the place where Java starts running your code?
- main method
- class name
- semicolon
Show answer
main method: Yes. main is the starting point.
A tiny Java program
Here is the structure. First comes the class, then main, then the statement that does the work.
The print statement sends text to the console. That is how we see output.
The braces group things together. Java uses them to know what belongs inside the class and inside main.
What prints first?
In System.out.println("Hi");, what gets sent to the console?
- Hi
- System.out.println
- main
Show answer
Hi: Correct. The text inside the quotes is what prints.
Statements end with semicolons
Most Java statements end with a semicolon.
That semicolon tells Java, 'this instruction is finished.'
Semicolon check
What do you usually put at the end of a Java statement?
- semicolon
- comma
- colon
Show answer
semicolon: Exactly. Most Java statements end with a semicolon.
Why braces matter
Braces make blocks. A block is just a group of code that belongs together.
This matters because Java needs to know what is inside the class and what is inside main.
A statement is one action inside the block. Java reads statements one by one.
Indenting helps you read code
Indenting is for humans, not for Java. But it makes the structure easier to see.
When you line up the code nicely, you can spot the blocks faster.
Your turn: spot the start
Which line should go inside main?
- System.out.println("Hello");
- public class Main {
- }
Show answer
System.out.println("Hello");: Yes. The print statement goes inside main.
Watch out for two common slips
Two common mistakes show up a lot. One is putting runnable code outside main.
The other is forgetting the semicolon. Java will complain, and that is your clue to check the line.
Use this checklist
Here is the checklist. First, find the class and the main method.
Then put the running statements inside main.
Finally, end each statement with a semicolon. If you do those four things, you are off to a strong start.