Java Classes & Objects in Java
Java is an Object-Oriented Programming (OOP) language. A Class is a blueprint or template that defines attributes (fields) and behaviors (methods). An Object is an actual instance created in memory from that blueprint using the `new` keyword.
"A Class is like an architect’s blueprint for a house. The blueprint is not a house you can live in; but using the blueprint, you can build 100 distinct physical houses (Objects), each with its own front door color and furniture."
Deep Dive: How It Works
Fields (Instance Variables): Define the state or attributes of an object (e.g. `String name; int balance;`).
Methods (Instance Methods): Define what actions the object can perform (e.g. `deposit()`, `display()`).
Instantiation: `Car myCar = new Car();` allocates memory on the heap and returns a reference.
Dot notation: Access fields and invoke methods using `myCar.speed` and `myCar.drive()`.
Syntax Blueprint
class Student {
String name;
int grade;
void study() { System.out.println(name + " is studying."); }
}
Student s1 = new Student();
s1.name = "Alex";
s1.study();Define fields and methods inside class. Instantiate with new keyword.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to access instance fields without creating an object: Car.speed = 50;Why it happens: Instance fields require a specific object instance (myCar.speed = 50;).
How to fix: Instantiate with new before accessing instance members.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Instantiate a Book object
Create a Book object named "b1".
Set b1.title = "Clean Code" and b1.pages = 464.
Print "Book: " + b1.title + " (" + b1.pages + " pages)".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.