Organizing Classes
& Files in Java
Learn how to split your code across multiple classes and files for cleaner, more maintainable, and professional-grade Java programs.
Why Organize Your Code?
Imagine writing a 1,000-line essay as a single paragraph: no headings, no chapters, no sections. Impossible to read, impossible to fix. The same is true for code. Splitting your program into multiple classes and files makes it:
What Is a Class?
A class is a blueprint that bundles together data (fields) and behavior (methods) into one logical unit. Think of it like a recipe card: it describes what something is and what it can do.
The One Class, One File Rule
Java enforces a fundamental rule: each public class must live in its own file, and the filename must match the class name (including capitalization).
Anatomy of a Java File
Every .java file follows the same three-part structure, from top to bottom:
The Core Principle: Separation of Concerns
Separation of Concerns means each class should have one clear responsibility. A class that tries to do everything becomes a tangled mess. Instead, split responsibilities across classes:
Classes vs. Objects
A common point of confusion: a class is the blueprint, while an object is a concrete instance built from that blueprint. You define a class once, then create many objects from it.
| Class (Blueprint) | Object (Instance) | |
|---|---|---|
| Definition | A template describing fields and methods | A specific thing created from the template |
| Analogy | Cookie cutter | An actual cookie |
| Keyword | class Dog { ... } | new Dog() |
| How many? | Defined once per file | Created as many times as needed |
How to Split Your Code
Example 1: Two-Class Program
Let's build a simple program with a Student class and a Main class.
Example 2: Three-Class Program
A pet shop tracker: Pet stores data, PetShop manages a list, Main runs everything.
Example 2: PetShop & Main
Grouping with Packages
As projects grow, you group related classes into packages: folders that give classes a namespace. This prevents naming conflicts and adds structure.
Access Modifiers: Who Can See What?
When splitting code across files, access modifiers control which parts of a class are visible to other classes:
| Modifier | Same Class | Same Package | Other Packages |
|---|---|---|---|
| public | ✓ | ✓ | ✓ |
| protected | ✓ | ✓ | ✕ (only subclasses) |
| (default) | ✓ | ✓ | ✕ |
| private | ✓ | ✕ | ✕ |
Example 3: Encapsulation in Action
Here is a BankAccount class that hides its balance field and controls access through methods:
Common Mistakes to Avoid
Quick Reference Summary
| Concept | What It Does | Example |
|---|---|---|
| Class | Blueprint for objects | public class Dog { } |
| .java File | Stores exactly one public class | Dog.java |
| Package | Folder grouping related classes | package models; |
| Import | Brings in external classes | import models.Dog; |
| Access Modifier | Controls visibility | private, public |
| main() Method | Program entry point | public static void main() |
Question 1 of 5
A public class named Vehicle must be saved in which file?
Question 2 of 5
How many public classes can a single .java file contain?
Question 3 of 5
Two classes, Order and Product, are in the same folder with no package declarations. Does Order.java need to import Product?
Question 4 of 5
Which access modifier should you use for a field that only its own class should be able to read or modify?
Question 5 of 5
You have three classes: Player, Game, and Scoreboard. According to the separation of concerns principle, which class should contain the main() method?