Classes & Objects — Blueprints and Instances
A class is a blueprint that defines the structure and behaviour of things. An object is one specific instance created from that blueprint. The moment you understand this distinction, Java's entire design starts to make sense. Every Java program is built from objects interacting with each other.
Class Anatomy
- Fields (attributes) — the data an object holds: title, price, available
- Methods (behaviour) — what an object can do: borrow(), returnBook(), getDetails()
- Constructor — a special method that initialises fields when the object is created
- Class name in PascalCase: Book, LibraryMember, OrderItem
- One class per file (public class must match filename): Book.java contains public class Book
Creating a Class & Instantiating Objects
// File: Book.java
public class Book {
// Fields — every Book has these
String title;
String author;
String isbn;
double price;
boolean available;
// Public method — behaviour
public void displayInfo() {
System.out.printf("[%s] by %s — $%.2f — %s%n",
title, author, price, available ? "Available" : "Checked Out");
}
}
// File: LibraryCatalog.java
public class LibraryCatalog {
public static void main(String[] args) {
// Instantiate objects using 'new'
Book book1 = new Book(); // 'new' calls constructor, allocates heap memory
book1.title = "Clean Code";
book1.author = "Robert Martin";
book1.isbn = "978-0132350884";
book1.price = 39.99;
book1.available = true;
Book book2 = new Book();
book2.title = "Effective Java";
book2.author = "Joshua Bloch";
book2.price = 44.99;
book2.available = false;
// Call methods on objects
book1.displayInfo(); // [Clean Code] by Robert Martin — $39.99 — Available
book2.displayInfo(); // [Effective Java] by Joshua Bloch — $44.99 — Checked Out
// Two variables can reference the SAME object
Book alias = book1;
alias.price = 29.99; // modifies book1's price too!
System.out.println(book1.price); // 29.99 — same object in heap
}
}Quick Quiz
Tip
Tip
Practice Classes Objects Blueprints and Instances in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Java is built on these four fundamental OOP principles
Practice Task
Note
Practice Task — (1) Write a working example of Classes Objects Blueprints and Instances from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Classes Objects Blueprints and Instances is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- A class is a blueprint that defines the structure and behaviour of things.
- Fields (attributes) — the data an object holds: title, price, available
- Methods (behaviour) — what an object can do: borrow(), returnBook(), getDetails()
- Constructor — a special method that initialises fields when the object is created