Skip to content
Laviyx Learn

Try

Java course menu

Java Syntax and Program Structure: Classes, Statements, Blocks and Packages

Learn the basic rules of Java syntax: classes and files, statements and semicolons, blocks and braces, case sensitivity, identifiers and naming conventions, packages and imports, and how a Java program is organized.

Beginner7 min readUpdated
On this page

Java has strict, consistent grammar. Once you know a handful of rules, every program you read follows the same shape.

Everything is inside a class#

Java code always lives in a class. A program needs at least one class with a main method:

Java
public class Store {
    public static void main(String[] args) {
        int apples = 5;
        int oranges = 3;
        System.out.println(apples + oranges);
    }
}
Output
8

Rules for files:

  • A file may contain several classes, but at most one public class.
  • The public class's name must match the file name: Store lives in Store.java.

Statements and semicolons#

A statement is one instruction, and it ends with a semicolon. Line breaks do not end statements, so a long statement can span several lines:

Java
public class Main {
    public static void main(String[] args) {
        String message = "This statement "
                + "continues on "
                + "three lines.";
        System.out.println(message);
    }
}
Output
This statement continues on three lines.

Blocks and braces#

A block groups statements with { and }. Classes, methods, if statements and loops all have blocks. The compiler ignores indentation, but humans rely on it: indent each level by 4 spaces.

Java
public class Main {
    public static void main(String[] args) {
        int temperature = 34;
        if (temperature > 30) {
            System.out.println("It's hot.");
            System.out.println("Drink water.");
        }
    }
}
Output
It's hot.
Drink water.

Case sensitivity and identifiers#

Java is case-sensitive: total, Total and TOTAL are three different names. Names you choose (identifiers):

  • may contain letters, digits, _ and $;
  • cannot start with a digit;
  • cannot be a keyword such as class, int, public or return.

Naming conventions#

Java code follows strong, almost universal conventions. Following them makes your code look professional and lets others read it instantly:

KindConventionExamples
Classes, interfaces, records, enumsPascalCaseBankAccount, HelloWorld
Variables and methodscamelCasetotalPrice, calculateTax()
ConstantsUPPER_SNAKE_CASEMAX_USERS, TAX_RATE
Packagesall lowercasecom.laviyx.shop

Packages and imports#

Classes are organized into packages, like folders. The standard library's classes live in packages such as java.util (collections, Scanner) and java.time (dates). To use a class from another package, import it at the top of the file:

Java
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> cities = new ArrayList<>();
        cities.add("Pune");
        cities.add("Delhi");
        LocalDate date = LocalDate.of(2026, 10, 23);
        System.out.println(cities + " " + date.getYear());
    }
}
Output
[Pune, Delhi] 2026
  • java.lang (which contains String, System, Math and Integer) is imported automatically.
  • import java.util.*; imports every class in a package. Many teams prefer explicit imports, which make dependencies clear; IDEs add them for you.

In real projects, each file starts with a package declaration such as package com.laviyx.shop;, and the folder structure matches the package name. Small single-file programs, like the ones in this course, can skip it.

The structure of a typical file#

Java
package com.laviyx.shop;              // 1. package (optional for small programs)

import java.util.List;                // 2. imports

public class Order {                  // 3. one public class per file
    private final List<String> items; // fields

    public Order(List<String> items) {   // constructor
        this.items = items;
    }

    public int itemCount() {          // methods
        return items.size();
    }
}

You will learn fields, constructors and methods in the OOP section, starting with Classes and Objects.

Common mistakes#

Practice#

Practice exercise

Write a class Rectangle with a main method that stores a width of 7 and a height of 4 in variables named by Java conventions, and prints Area: 28.

Show solution
Java
public class Rectangle {
    public static void main(String[] args) {
        int width = 7;
        int height = 4;
        System.out.println("Area: " + width * height);
    }
}
Output
Area: 28

width * height is calculated before the string is joined, because * has higher precedence than +.

Key takeaway#

Finished this lesson?