Java Syntax & Program Structure
Every Java source file follows the same shape, top to bottom: an optional package declaration, then optional import statements, then exactly one top-level public class whose name matches the file name. Inside that class, fields (data) and methods (behavior) can appear in any order β the compiler doesn't care which comes first. Every statement ends with a semicolon, and every block β a class body, a method body, a loop body β is delimited by curly braces.
Indentation inside those braces is purely a human readability convention; unlike Python, the compiler doesn't read whitespace as structure at all. Community convention is still strong, though: PascalCase for class names, camelCase for methods and variables, and SCREAMING_SNAKE_CASE for constants are followed almost universally, even though nothing forces it.
Package declaration (optional)
If present, package com.example.app; must be the very first non-comment line β it declares which namespace this file's classes belong to.
Import statements (optional)
import java.util.List; brings a class into scope so you can refer to it by its simple name instead of writing the fully-qualified path every time.
The class declaration
Exactly one top-level public class per file, and its name has to match the file name exactly.
The class body
Fields and methods live inside the class's braces in whatever order you write them β order has no effect on how the program runs.
Statements and blocks
Every statement ends with a semicolon, and every block is wrapped in { }; indentation inside those braces is a style choice, never something the compiler parses.
package com.example.app;Β import java.util.List;Β public class Config { static final String VERSION = "1.0";Β public static void main(String[] args) { List<String> features = List.of("logging", "metrics"); System.out.println("App v" + VERSION); System.out.println("Features: " + features); }}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself β building beats watching.