Java Formatting
Arrange code so a reader can see blocks and operators at a glance. In Yard.java, keep the public class Yard. Inside main, indent the body lines, write int sum = 10 + 2; with spaces around = and +, then print sum. Compile with javac Yard.java and run java Yard. The screen should show 12. Leave dates for the next page — this lesson is spacing and indent, not calendar types.
Indent inside the braces
The naming page proved exact spellings. This page proves tidy layout. Open Yard.java. Keep the public class name Yard. Delete leftover ticketCount / TicketCount demos if they bury the formatting story, leftover Javadoc-only add files, leftover debug label hunts, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Write the class and main with braces that mark a block. Put the statements that belong to main one indent level deeper than the main line. A common beginner habit is four spaces per level. Keep the same width on every indented line in this demo.
Keep spaces around the assignment and the plus sign so the eyes can split names from operators:
int sum = 10 + 2;
Then print the result:
System.out.println(sum);
Formatting does not change the math. Ten plus two is still twelve. The indent and spaces help a human scan which lines sit inside main.
public class Yard {
public static void main(String[] args) {
int sum = 10 + 2;
System.out.println(sum);
}
}
From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard.
The screen shows 12. Walk the run once. Java starts at Yard.main. The indented lines are the body. The assignment stores twelve under sum. The print writes 12. If you remove the spaces around = and still compile, the result can stay 12 — the spaces were for readers. If you remove required braces or break the structure, javac complains. This page keeps a complete, readable shape on purpose.
Say the rule while you watch the screen: indent the body, space the operators you want eyes to catch, then compile and run to prove the program still works. Do not invent date and time APIs on this page. Dates wait for the next lesson.
If you still see 4 then 9 from a naming file, switch to the formatting teaching file and run java Yard again. If every line sits at column zero with no indent, the program might still run, but the block boundaries are harder to read. Keep the indent in the teaching file.
A short formatting map
The short map below is the short map of this page. First you indent the body. Then you keep spaces around clear operators. Then you verify with javac and java Yard. Skip guessing that spaces change the sum. Skip deleting all indent before you know the screen still shows 12.
📊 Beginner formatting habits on Yard
| Habit | What you do |
|---|---|
| Indent | Push main body lines one level deeper than main |
| Operators | Keep spaces around = and + in simple assignments |
| Verify | Compile and run so the screen still shows 12 |
Keep the table beside the teaching file while you edit. Point at the opening brace of main, then at the indented sum line, then at the closing brace. That matching game is the whole beginner idea in one pass. Tools that auto-format later follow similar habits. Humans use the same map when the file grows.
If leftover naming demos still sit nearby, keep them as history. For this lesson run the formatting teaching file so the screen shows a tidy sum, not a case-sensitivity pair. If javac complains about duplicate public classes, keep only Yard public.
Formatting is not the math
A crowded line can still compute the same sum. int sum=10+2; without spaces may compile and print 12. This page still teaches spaces because readers tire less when operators stand out. Indent is the same idea for blocks: the compiler cares about braces; readers care about seeing which lines belong together.
Do not treat formatting as a second programming language. Keep one clear style in the teaching file. Do not mix tabs and spaces in a way that makes lines jump. Four spaces per level is enough for this beginner shape.
When the layout is wrong but the tokens are right, javac may still succeed. That is why you read the braces and the indent together, not only the screen number.
Compared with naming spellings
On the naming page, ticketCount and TicketCount were different names. On this page, spaces and indent do not create new names. They arrange the same names and operators so the block is obvious. You still compile Yard.java and run java Yard. The new habit is consistent indent inside braces plus readable operator spacing.
Naming answered which spelling to look up. Formatting answers how the file should look while you look. Keep both habits later. This lesson isolates layout so the screen proves the sum after a tidy edit.
If a leftover naming file is still open, you may see 4 and 9. Switch to the formatting file so main prints sum as 12. Keep date demos off this page. Keep keyboard input off this page.
Before dates
You can indent main, space = and +, store sum as 10 + 2, and see 12 after java Yard. Compile Yard.java and run java Yard for the kept demo. The layout helped readers; the math stayed twelve. The next page is dates, where you work with calendar values in Java.
Keep the tidy sum demo for a clear layout-plus-run pair. Keep naming files off this page when you practice indent. Keep dates files off this page until that lesson. Those three habits are the checkpoint before you move on. When the braces show the block and the screen shows 12, formatting did its job for this lesson and you are ready for the next topic about dates.
One last pass through the folder: one public Yard, indented main body, spaced operators, no keyboard wait, no forced date types. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still shows 12 and the indent still marks the body, the formatting shape is solid and the course can move to dates on the next page. Prefer one clear layout habit per read so each pass teaches the same map.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top