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.
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:
public class Store {
public static void main(String[] args) {
int apples = 5;
int oranges = 3;
System.out.println(apples + oranges);
}
}8Rules for files:
- A file may contain several classes, but at most one
publicclass. - The public class's name must match the file name:
Storelives inStore.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:
public class Main {
public static void main(String[] args) {
String message = "This statement "
+ "continues on "
+ "three lines.";
System.out.println(message);
}
}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.
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.");
}
}
}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,publicorreturn.
Naming conventions#
Java code follows strong, almost universal conventions. Following them makes your code look professional and lets others read it instantly:
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:
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());
}
}[Pune, Delhi] 2026java.lang(which containsString,System,MathandInteger) 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#
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
public class Rectangle {
public static void main(String[] args) {
int width = 7;
int height = 4;
System.out.println("Area: " + width * height);
}
}Area: 28width * height is calculated before the string is joined, because * has higher precedence than +.
Key takeaway#
Finished this lesson?