Metadata Card
- Prerequisites: Ch2 (Variables & Types), Ch5 (Methods)
- Estimated time: 65 min
- Core difficulty:
- Reading mode: Steady progress
- Completion marker: Can define classes, create objects, understand reference vs value; use constructors, this, static, encapsulation, access modifiers
The Breakthrough · Tracing the Origins
Master Chen pulls you aside: "You've been storing book data as five parallel arrays — title, author, ISBN, price, borrowed status. That works for one book, but fifty? What happens when you need to sort by title and all five arrays go out of sync?"
His point: Data has structure. A book is a single concept. Don't split it into separate arrays. Define a Book class, then create objects from it.
Act 1: From Blueprint to Reality — Classes and Objects
// Define a "Book" class — this is the blueprint
class Book {
// Fields (properties): what data describes a book
String title;
String author;
String isbn;
double price;
boolean isBorrowed;
}Language: Java How to run: Save as Book.java, compile. Don't write main yet — this is just a type definition.
Now create an actual book from this blueprint:
public class Library {
public static void main(String[] args) {
Book myBook = new Book();
myBook.title = "Core Java";
myBook.author = "Horstmann";
myBook.isbn = "978-7-111-12345-6";
myBook.price = 99.00;
myBook.isBorrowed = false;
System.out.println(myBook.title); // Core Java
System.out.println(myBook.price); // 99.0
}
}What Book myBook = new Book() does:
- Declares a variable
myBookthat can hold aBookreference (on the stack) - Allocates space on the heap for the
Bookobject (fields default to null/0/false) - Assigns the heap address to
myBook
The variable doesn't contain the object — it points to it.
Act 2: Constructors
class Book {
String title;
String author;
// Constructor: same name as class, no return type
Book(String title, String author) {
this.title = title;
this.author = author;
}
}
// Creating with constructor:
Book b = new Book("Core Java", "Horstmann");Why constructors? Without one, you'd manually set every field. With a constructor, you initialize in one step. If you define any constructor, the default no-arg constructor disappears.
Act 3: This, Static, Encapsulation
this — refers to the current object. Resolves name conflicts:
class Student {
String name;
void setName(String name) {
this.name = name; // this.name = field, name = parameter
}
}static — belongs to the class, not instances:
class Book {
static int totalCount = 0;
Book() { totalCount++; }
}
// Book.totalCount — class-level, shared across all instancesEncapsulation — hide data, expose safe access:
class Book {
private String title;
public String getTitle() { return title; }
public void setTitle(String title) {
if (title == null || title.isEmpty()) {
throw new IllegalArgumentException("Title cannot be empty");
}
this.title = title;
}
}Packages and Access Modifiers
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
private | ||||
| default | ||||
protected | ||||
public |
Final Challenge
- Define a
Studentclass with private fieldsname,age,score. Provide constructor and getter/setter - Add a static
countfield tracking how many students are created - Create a
BankAccountclass with deposit/withdraw/transfer methods, validating all operations
Traveler's Notes
Class = blueprint. Object = instance from the blueprint.
Constructors initialize objects at creation time.thisrefers to the current object;staticbelongs to the class.
Encapsulation = private fields + public methods.