Command Palette

Search for a command to run...

Phase 4Beginner5 of 17 in Core Java

Classes and Objects

Design your own types: classes, objects, constructors, this, encapsulation, static, packages, equals/hashCode, records, enums and immutability.

Until now every program lived inside one main method and worked with numbers, strings and arrays. Real programs model things: a bank account, a parcel, a player, an order. A class lets you invent a new type for that thing, and an object is one real copy of it living in memory. This phase is where Java stops being a calculator and starts being a language for building systems.

You'll learn how objects are created and laid out on the heap, how constructors set them up safely, why fields are made private, what static really means, how packages organise code, and the equals/hashCode contract that every HashMap in the world depends on. Then you'll meet the modern tools that remove boilerplate: records (Java 16) and enums, and finish with immutable objects and what happens to an object when nothing points to it any more.

Every topic builds on Phase 3 (methods, arrays, pass-by-value, stack and heap). Phase 5 builds directly on this one with inheritance and polymorphism, and Phase 9 (collections) relies on the equals/hashCode rules you learn in Topic 4.8.

0/12 · 0%
12 topics ~6 h 42 code blocks & diagrams
Start with the first topic
1
4.1

Classes and Objects

A class is a blueprint that describes what data a kind of thing holds and what it can do. An object is one real thing built from that blueprint with new, living on the heap, reached through a reference.

30 min 4 code practice

2
4.2

Fields and Instance Methods

Fields are an object's memory: the data it carries between method calls. Instance methods are the object's behaviour: code that reads and changes that object's own fields, so the data and the rules for it live together.

28 min 3 code practice

3
4.3

Constructors and Constructor Chaining

A constructor is the special block that runs once when an object is created with new, so the object starts life complete and valid. Constructors can be overloaded and can call each other with this(...), keeping the setup logic in one place.

29 min 4 code practice

4
4.4

The this Keyword

this is a reference to the object the current method or constructor is running on. You use it to tell a field from a parameter with the same name, to pass or return the current object, and (as this(...)) to call another constructor.

26 min 3 code practice

5
4.5

Encapsulation and Access Modifiers

Encapsulation means an object hides its data and lets the outside world change it only through methods that enforce the rules. Java's access modifiers (private, package-private, protected, public) decide exactly which code can see each class, field, method and constructor.

29 min 3 code practice

6
4.6

static Fields, Methods and Blocks

static means "belongs to the class itself, not to any one object". A static field has exactly one copy shared by everyone, a static method runs without an object, and a static block runs once when the class is first initialised.

29 min 4 code practice

7
4.7

Packages and Imports

A package is a named group of related classes, stored in a matching folder, that gives every class a unique full name such as java.util.ArrayList. import lets you write the short name instead, and package-private access lets classes in one package share helpers without exposing them.

27 min 4 code practice

8
4.8

toString, equals and hashCode

Every class inherits toString, equals and hashCode from Object. Override toString to get readable output, and override equals and hashCode together, following their contract, whenever two different objects should count as "the same value", because HashMap, HashSet and most of the collections library depend on it.

33 min 4 code practice

9
4.9

Records

A record is a short way to declare a class that is just a carrier for immutable data. From one line, record Point(int x, int y) {}, Java generates private final fields, a constructor, accessor methods, and correct equals, hashCode and toString.

32 min 4 code practice

10
4.10

Enums

An enum is a class with a fixed, named set of instances, such as SMALL, MEDIUM, LARGE. Enums replace error-prone int and String constants with a real type the compiler checks, and they can carry fields, constructors and behaviour.

30 min 3 code practice

11
4.11

Immutable Objects

An immutable object can't change after it's constructed: every "change" returns a new object. Immutable objects are simple to reason about, safe to share between threads, and safe as HashMap keys, which is why String, Integer, LocalDate and records are built that way.

28 min 3 code practice

12
4.12

Object Lifetime, References and null

An object lives as long as some chain of references from the running program can reach it; after that the garbage collector may reclaim it, at a time you don't control. null means "no object", and using it as if it were one throws NullPointerException.

30 min 3 code practice