Classes and Objects in Java: The Foundation of OOP
Understand Object-Oriented Programming in Java from scratch — what classes and objects are, how to write a class, create objects with new, and use fields and methods.
Welcome to Level 2 — and to the idea that makes Java Java: Object-Oriented Programming, or OOP. Everything you've written so far has lived inside main. Real applications are built differently — out of objects that model real-world things. This lesson introduces the two most important words in all of Java: class and object. Get these, and the rest of OOP falls into place.
The big idea: classes are blueprints, objects are the real things
Think about a car factory. There's one blueprint that describes what a car is — it has a colour, a brand, a speed, and it can drive and brake. From that single blueprint, the factory builds many actual cars, each with its own colour and brand.
- A class is the blueprint — it defines what something has and what it can do. There's just one.
- An object is a real thing built from that blueprint — you can make as many as you want, each with its own data.
Writing a class
A class bundles together two things: fields (the data it has) and methods (the things it can do). Here's a simple Car class:
public class Car { // fields — what a car HAS String brand; String color; int speed; // method — what a car can DO void drive() { System.out.println(brand + " is driving."); } }
That's the blueprint. Notice it has no main — a class is just a definition. Now let's build actual cars from it.
Creating objects with new
You create an object using the new keyword. Each object gets its own copy of the fields:
public class Main { public static void main(String[] args) { Car car1 = new Car(); // build a car object car1.brand = "Honda"; // set its fields with the dot car1.color = "red"; Car car2 = new Car(); // a totally separate car car2.brand = "Tata"; car1.drive(); // Honda is driving. car2.drive(); // Tata is driving. } }
The dot (.) is how you reach into an object — car1.brand reads or sets that car's brand, and car1.drive() calls its method. car1 and car2 are independent: changing one doesn't touch the other.
Objects you've already used
Here's something reassuring: you've been using objects since Lesson 6. Scanner sc = new Scanner(System.in) creates a Scanner object, and sc.nextInt() calls a method on it. Strings are objects too. So this isn't new territory — you're just learning to build your own now.
Why bother? The point of OOP
OOP lets you model your program around real concepts — a User, an Order, a BlogPost — instead of a tangle of loose variables. This website is a perfect example: it has a Blog class, a Category class, a User class, and so on. Each bundles its own data and behaviour, which keeps a large codebase understandable. That's exactly the organisation you're learning to build here.
When OOP first clicked for me, it was through this exact car analogy — one blueprint, many objects. Up to that point I'd been writing everything in main and it always felt cramped. The moment I started modelling my programs as objects, they suddenly matched how I actually thought about the problem. Stick with it — this is the doorway to writing real software.
Key takeaways
- A class is a blueprint; an object is a real instance built from it with
new. - Classes hold fields (data) and methods (behaviour).
- Use the dot (
object.field,object.method()) to access an object's data and actions. - Each object has its own independent copy of the fields.
← Previous: Lesson 11 — Strings in Java
Next: Lesson 13 — Constructors in Java →