Learn Java
import com.learnjava.gettingstarted.SyntaxStructure;

Java Syntax & Program Structure

@Concept

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.

@HowItWorks
1

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.

2

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.

3

The class declaration

Exactly one top-level public class per file, and its name has to match the file name exactly.

4

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.

5

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.

@Example
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);
}
}
Output
App v1.0
Features: [logging, metrics]
1package com.example.app; places this file's class inside that namespace β€” it has to be the very first non-comment line.
2import java.util.List; lets the code write List instead of java.util.List every time it's used.
3The VERSION field and the main method both live inside the same pair of braces that opens right after public class Config.
4main runs top to bottom: it builds a list, then two println calls each print one line, for two lines of output total.
@UseCases
Reading unfamiliar Java source by quickly telling apart package and import lines from the actual logic
Debugging a 'class X is public, should be declared in a file named X.java' compiler error
Structuring a new file correctly before an IDE's auto-formatter gets a chance to fix it
Explaining Java's brace-and-semicolon layout to someone coming from a whitespace-sensitive language

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself β€” building beats watching.