OOP in Java: Classes and Objects.
Every Java tutorial throws the word “class” at you in the first five minutes. Most never explain what it actually means. This one does.
OOP in Java: Classes and Objects.
Every Java tutorial throws the word “class” at you in the first five minutes. Most never explain what it actually means. This one does.

My first week learning Java, I copied a “Hello World” program from a tutorial. It had the word class at the top. The tutorial said: "In Java, everything lives inside a class." I nodded and moved on. I had no idea what that meant. I just accepted it as one of those things you type without understanding.
Three months later I was writing actual code, making actual mistakes, and one day something clicked. A class is not just a Java rule. It is a way of thinking about problems. Once I understood that, everything else in Java started making sense.
This article is the explanation I wish I had in week one.
Have you ever read the word “class” in Java code and just accepted it without really knowing what it means? Most beginners do. By the end of this article, that will change.
What a class actually is
Before any code, let us talk about the idea. A class is a blueprint. It describes what something looks like and what it can do. The thing itself, created from that blueprint, is called an object.
Think about a car. Before any car exists, someone designs a blueprint. That blueprint says every car has a color, a model name, and a fuel level. It also says every car can start, accelerate, and stop. The blueprint is the class. Each actual car built from it is an object.
One blueprint. Many objects. Each object has its own data but follows the same structure.
In Java, you write a class once. Then you create as many objects from it as you need. Each object has its own values but shares the same set of properties and behaviors defined in the class.
Class vs Object — the simplest way to see it
A class is the recipe. An object is the dish you make from it. You write the recipe once. You can cook the dish a hundred times. Each dish is separate, but they all follow the same instructions.
Writing one from scratch
Here is the simplest possible class in Java. It represents a car. Read through it once before I explain each part.
public class Car {
// Fields — the data this class holds
String colour;
String model;
int fuelLevel;
// Method — something this class can do
void start() {
System.out.println(model + " is starting...");
}
void refuel(int amount) {
fuelLevel = fuelLevel + amount;
System.out.println("Fuel level: " + fuelLevel);
}
}
That is a complete class. It has three fields (colour, model, fuelLevel) and two methods (start, refuel). Fields store data. Methods define behaviour.
Right now this class just sits there. It does nothing on its own. You need to create an object from it to actually use it.
Creating objects bringing the class to life
Creating an object from a class is called instantiation. You use the new keyword to do it. Each object you create gets its own copy of the fields defined in the class.
public class Main {
public static void main(String[] args) {
// create first object from the Car class
Car myCar = new Car();
myCar.colour = "Red";
myCar.model = "Swift";
myCar.fuelLevel = 50;
// create second object from the same Car class
Car friendsCar = new Car();
friendsCar.colour = "Blue";
friendsCar.model = "Creta";
friendsCar.fuelLevel = 30;
// each object has its own data
myCar.start(); // Swift is starting...
friendsCar.start(); // Creta is starting...
// refueling one does not affect the other
myCar.refuel(20);
System.out.println(myCar.fuelLevel); // 70
System.out.println(friendsCar.fuelLevel); // 30, unchanged
}
}
Two objects, same class. Each one has its own colour, model, and fuel level. Changing one does not touch the other. That is the point of objects — they are independent instances of the same blueprint.
Constructors
Setting up an object properly from the start
In the example above, we set the fields manually after creating the object. That works but it is messy. A constructor is a cleaner way to set up an object the moment it is created.
A constructor is a special method that runs automatically when you use new. It has the same name as the class and no return type.
public class Car {
String colour;
String model;
int fuelLevel;
// Constructor — runs when you write: new Car(...)
public Car(String colour, String model, int fuelLevel) {
this.colour = colour;
this.model = model;
this.fuelLevel = fuelLevel;
}
void start() {
System.out.println(model + " is starting...");
}
void describe() {
System.out.println(
model + " | " + colour + " | Fuel: " + fuelLevel
);
}
}
// Now creating an object is one clean line
Car myCar = new Car("Red", "Swift", 50);
Car friendsCar = new Car("Blue", "Creta", 30);
myCar.describe(); // Swift | Red | Fuel: 50
friendsCar.describe(); // Creta | Blue | Fuel: 30
What is “this”?
Inside a constructor,
thisrefers to the current object being created. When you writethis.colour = colour, you are saying: set this object's colour field to the value passed in. It distinguishes between the field name and the parameter name when they are the same.
Real world use
How this connects to your Java work
If you write Selenium tests, you already use classes and objects every single day — even if you did not realise it. Every time you write new ChromeDriver(), you are creating an object from the ChromeDriver class. Every time you write driver.findElement(), you are calling a method on that object.
// WebDriver is a class. driver is an object created from it.
WebDriver driver = new ChromeDriver();
// By is a class. By.id("email") creates an object.
WebElement email = driver.findElement(By.id("email"));
// WebDriverWait is a class. wait is an object.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Every time you write "new Something()" you are creating an object.
// The class defines what it can do.
// The object is the thing that actually does it.
You have been working with classes and objects since your first Selenium test. Now you know exactly what is happening under the hood.
The Page Object Model is built on this idea
In Selenium automation, the Page Object Model uses one class per page of the application. Each class holds the locators and actions for that page. When your test needs to use a page, it creates an object from that class. That is OOP in direct practice. We cover this fully in Part 6 of this series.
Common mistakes What beginners usually get wrong
The most common mistake is confusing the class with the object. The class is the definition. The object is the thing. Writing Car myCar does not create a car. Writing Car myCar = new Car() does.
The second mistake is forgetting that each object is independent. If you change a field on one object, it does not change the same field on other objects created from the same class. They share the blueprint, not the data.
// Mistake — declaring without creating
Car myCar;
myCar.start(); // NullPointerException — no object exists yet
// Fix — always use new to create the object
Car myCar = new Car("Red", "Swift", 50);
myCar.start(); // works fine now
// Mistake — thinking objects share data
Car car1 = new Car("Red", "Swift", 50);
Car car2 = new Car("Blue", "Creta", 30);
car1.fuelLevel = 100;
System.out.println(car2.fuelLevel); // still 30, not 100
A class is a blueprint. An object is what you build from it. Fields store data. Methods define behaviour. Constructors set everything up cleanly from the start.
That is the whole foundation. Everything else in Java OOP builds on top of this. Encapsulation, inheritance, polymorphism, abstraction — they are all just ways of organising and managing classes and objects more effectively.
Which part of this clicked for you today? And which part still feels unclear? Share it below — your question is probably the same one ten other readers have but did not ask.
Resources
- Java classes and objects — Oracle official tutorial
- Java classes explained — W3Schools
- Classes and objects in Java — GeeksforGeeks
If this article helped you, consider clapping and following for more practical SDET content.
Connect
Java #OOP #JavaBeginner #ObjectOrientedProgramming #Classes #CoreJava #Programming #SDET #LearnJava #SoftwareEngineering
메타데이터
- post_id
- 2e64c1b0519b
- slug
- oop-in-java-classes-and-objects-2e64c1b0519b
- url
- https://medium.com/@deepshah201/oop-in-java-classes-and-objects-2e64c1b0519b
- canonical_url
- https://medium.com/@deepshah201/oop-in-java-classes-and-objects-2e64c1b0519b
- author_url
- https://medium.com/@deepshah201
- status
- ok
- fetched_at
- 2026-06-12 18:14:10