Skip to content

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

java
// 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:

java
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:

  1. Declares a variable myBook that can hold a Book reference (on the stack)
  2. Allocates space on the heap for the Book object (fields default to null/0/false)
  3. Assigns the heap address to myBook

The variable doesn't contain the object — it points to it.

Act 2: Constructors

java
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:

java
class Student {
 String name;
 void setName(String name) {
 this.name = name; // this.name = field, name = parameter
 }
}

static — belongs to the class, not instances:

java
class Book {
 static int totalCount = 0;
 Book() { totalCount++; }
}
// Book.totalCount — class-level, shared across all instances

Encapsulation — hide data, expose safe access:

java
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

ModifierClassPackageSubclassWorld
private
default
protected
public

Final Challenge

  1. Define a Student class with private fields name, age, score. Provide constructor and getter/setter
  2. Add a static count field tracking how many students are created
  3. Create a BankAccount class with deposit/withdraw/transfer methods, validating all operations

Traveler's Notes

Class = blueprint. Object = instance from the blueprint.
Constructors initialize objects at creation time.
this refers to the current object; static belongs to the class.
Encapsulation = private fields + public methods.

Built with VitePress | Software Systems Atlas