Classes & Objects
Overview
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or 'Objects', rather than just functions and logic. The absolute foundation of OOP in Java relies on two concepts: Classes and Objects.
A Class is essentially a blueprint or template. It defines the structure, properties (attributes/variables), and behaviors (methods/functions) that an entity should have, but it does not actually exist in memory as tangible data.
An Object is a physical, usable 'Instance' of that class. When you use the new keyword, the JVM reads the Class blueprint, carves out physical space in the Heap Memory, and creates an Object. You can create 100 completely independent Objects from a single Class blueprint, each maintaining its own unique state (like 100 different Bank Account objects created from one BankAccount class).
Syntax
Notice how ferrari and civic share the exact same methods and attribute names because they come from the same Car blueprint, but their actual values are completely isolated from one another in memory.
// 1. THE CLASS (The Blueprint)
public class Car {
// Attributes (State)
String color;
int maxSpeed;
// Methods (Behavior)
public void drive() {
System.out.println("Driving at " + maxSpeed + " mph!");
}
}
// 2. THE OBJECTS (The Instances)
public class Main {
public static void main(String[] args) {
// Creating Object 1
Car ferrari = new Car();
ferrari.color = "Red";
ferrari.maxSpeed = 200;
// Creating Object 2 (Completely independent)
Car civic = new Car();
civic.color = "Blue";
civic.maxSpeed = 120;
// Triggering behavior
ferrari.drive(); // Prints: Driving at 200 mph!
civic.drive(); // Prints: Driving at 120 mph!
}
}Common Pitfalls
- Forgetting the
newkeyword. If you writeCar c;you have only created a variable capable of holding a Car, but it currently points tonull. If you try to callc.drive(), the application will crash with aNullPointerException. - Making every attribute
static. If you declarestatic String color;inside the Car class, the color is no longer tied to individual objects; it is tied to the Class itself. If you paint the Ferrari red, the Civic will instantly turn red too, destroying object independence.
Interview Questions
The actual Object data (the instance variables) is always created and stored in the Heap memory. The Reference Variable (the variable that holds the memory address pointing to the object) is stored on the Stack.
A Class is a logical template or blueprint used to define the state (variables) and behavior (methods) of an object. It does not consume Heap memory until it is instantiated into an Object using the new keyword.
Real-World Example
In an e-commerce backend, you create a single Product class blueprint. When a user views a catalog, the server queries the database and instantiates hundreds of individual Product objects, each populated with different names, prices, and images, before sending them to the frontend.
public class Product {
String id;
String name;
double price;
public void applyDiscount(double percent) {
this.price = this.price * (1 - percent);
}
}
// Generating catalog objects
Product laptop = new Product();
laptop.name = "MacBook Pro";
laptop.price = 2000.00;Check Your Knowledge
Test your understanding of Classes & Objects with these quick questions.