Java Tutorial: Classes & Objects
Create classes, constructors, instance fields, methods, this keyword, and the difference between classes and objects.
Build your own types
So far you've used Java's built-in types: `int`, `String`, `double`. Classes let you create YOUR OWN types — `BankAccount`, `Student`, `Product`.
A class is a blueprint. An object is the thing you build from that blueprint. Every `String` you've used is an object built from Java's `String` class.
What you'll learn in this Java classes & objects tutorial
This interactive Java tutorial has 6 hands-on exercises. Estimated time: 22 minutes.
- Your first class — A class defines fields (data) and methods (behaviour).
- Add a method to your class — Add `deposit(double amount)` and `getBalance()` methods to `BankAccount`.
- toString — readable objects — By default, printing an object gives something like `Dog@5f4da5c3` — the memory address. Useless.
- Multiple constructors — Like methods, constructors can be overloaded — multiple constructors with different parameters.
- Object references and null — Variables hold **references** to objects, not the objects themselves.
- Build a Product class — Final challenge — no starter code.
Java Classes & Objects concepts covered
- Build your own types