Theory
6000 books, 5 variables each
In Unit 1, BookBridge stored one book in loose variables: title, section, copies, price, available.
The college library holds about 6000 books. Following the Unit 1 style, that is 30000 variables, with nothing connecting title47 to copies47 except your discipline in naming them.
The facts of one book belong together. Java's unit of togetherness is the class, and this lesson finally gives BookBridge its Book.
Theory
The index card template
Old libraries kept an index card for every book: one printed template (Title: ___, Copies: ___) and thousands of filled-in cards.
A class is the printed template: it lists what facts every book has, but is itself no book. An object is one filled card: real values for one real book. Printing the template once and stamping many cards is exactly class Book once, new Book() many times.
Theory
Class and field, formally
A class is a user-defined blueprint that groups data and the methods that work on it. The data slots are called fields (or instance variables).
class Book {
String title;
int copies;
}
Create an object with new, reach its fields with the dot operator:
Book b1 = new Book();
b1.title = "Let Us C";
Note for BCA304 veterans: no semicolon after the class's closing brace in Java; C++ demanded one.
Practical
Two cards from one template
class Book {
String title; // fields: every Book gets its own
int copies;
}
public class BookBridge {
public static void main(String[] args) {
Book b1 = new Book();
b1.title = "Let Us C";
b1.copies = 5;
Book b2 = new Book();
b2.title = "Java Complete Reference";
b2.copies = 2;
b2.copies = b2.copies - 1; // issue one copy of b2 only
System.out.println(b1.title + " | " + b1.copies);
System.out.println(b2.title + " | " + b2.copies);
}
}
Theory
Each object owns its fields
Run the listing: b1 still shows 5 copies, b2 shows 1. Issuing b2's copy touched only b2's copies field.
That is the rule: every new allocates a fresh, independent set of instance fields. The class is loaded once; the data multiplies per object.
So "how many title variables exist after creating 3 Books?" has a precise answer: 3, one inside each object, plus 0 in the class itself.
Quiz
BookBridge creates 3 Book objects. How many separate copies of the title field exist in memory?
- 1, shared by all Book objects
- 3, one inside each object
- 0 until title is assigned a value
- Depends on which constructor was used
Show the answer
3, one inside each object
Instance fields belong to objects, so 3 objects mean 3 independent title slots; changing one never disturbs the others. Option A describes a static field, which you will meet later in this unit. Option C confuses assignment with allocation: the slot exists from the moment of new, holding a default value. Constructors (option D) decide initial values, not how many fields exist.
Think first
The field you never set
Book b = new Book(); System.out.println(b.copies); You never assigned copies. Before tapping: error, garbage value, or something else?
Show the answer
It prints 0. Java gives every field an automatic default: 0 for numeric types, false for boolean, null for references like String (so b.title would print null). Two contrasts worth marks: C gave you garbage, and Java's local variables inside methods get NO default; using one before assigning it is a compile error. Fields are born with defaults, locals are born empty.
Watch out
Blueprint is not building
Writing class Book { ... } allocates no memory for books. Students write "defining a class creates an object" and lose the mark: the class is only the plan; memory for fields appears at each new.
Second trap: Book b; alone declares a reference that points at nothing (null). Touching b.title before b = new Book(); compiles fine and then throws a NullPointerException at runtime, Java's most famous crash.
Theory
The card gets a front desk next
Right now any code can scribble on any card: b1.copies = -50; compiles happily. A real library guards its records: you request a change and the system applies the rules. That guarding is the next 2 lessons: access controls decide who may touch a field, and constructors make sure no Book is ever born half-filled. The CanteenTill from BCA304 followed the same path: data first, protection second.
Summary
Key takeaways
- A class groups related data (fields) and behaviour into one blueprint; an object is one live instance of it.
- class Book { String title; int copies; } defines the template; new Book() stamps a fresh object.
- Every object owns an independent copy of each instance field.
- Fields get automatic defaults (0, false, null); local variables get none.
- A declared but unassigned reference is null; using it throws NullPointerException.
- No semicolon after the class brace in Java, unlike C++.
- Memory hook: the class is the printed card, new stamps a fresh copy.