Objects and classes is the first idea in Java that trips people up. Not because it is hard. Because the way most books explain it makes it sound abstract and mysterious. It is not. You already understand the concept from everyday life. You just have not heard it explained in plain words yet.
This article will fix that. By the end you will know what a class is, what an object is, how they relate, and how to write both in Java. Real examples, no jargon for its own sake.
The Blueprint Idea
Think about an architect drawing a house plan. The plan on paper is not a house. You cannot live in it. But every house built from that plan looks the same in the important ways. Same number of rooms, same door positions, same roof shape.
A class is that plan. It is a blueprint that describes what something looks like and what it can do.
An object is an actual house built from that plan. You can touch it, walk inside, use it. Two houses built from the same plan are separate objects, but they share the same structure.
The official Java tutorials describe it this way: "An object is a software bundle of related state and behavior." A class is the template from which those objects are created.
State and Behavior
Every object has two things:
- State: the data it holds. For a phone, that is the brand, the model, the battery level, the phone number.
- Behavior: the actions it can perform. For a phone, that is making a call, sending an SMS, checking the battery.
In Java, state lives in fields (also called instance variables). Behavior lives in methods.
Think of a bKash account. The state is the balance, the account number, the owner name. The behavior is send money, cash out, pay a bill, check the balance. Same idea, different object.
Writing Your First Class
Let us turn the phone idea into actual Java code.
public class Phone {
String brand;
String model;
int batteryLevel;
void makeCall(String number) {
System.out.println("Calling " + number + " from " + model);
batteryLevel = batteryLevel - 1;
}
void chargeBattery() {
batteryLevel = 100;
System.out.println(model + " fully charged.");
}
}
Read this line by line. The class keyword tells Java you are defining a blueprint named Phone. Inside it, three fields describe the state. Two methods describe the behavior.
Notice nothing here actually creates a phone. This is just the plan. To use it, you need an object.
Creating an Object
Objects are created with the new keyword. Here is how you make a phone and use it.
public class Main {
public static void main(String[] args) {
Phone myPhone = new Phone();
myPhone.brand = "Samsung";
myPhone.model = "Galaxy A15";
myPhone.batteryLevel = 50;
myPhone.makeCall("01700000000");
System.out.println("Battery now: " + myPhone.batteryLevel);
myPhone.chargeBattery();
}
}
The line new Phone() is where the magic happens. Java allocates memory for a fresh Phone object and returns a reference to it. You store that reference in the variable myPhone.
From there you set the fields with dot notation (myPhone.brand = ...) and call methods the same way (myPhone.makeCall(...)).
The output looks like this:
Calling 01700000000 from Galaxy A15
Battery now: 49
Galaxy A15 fully charged.
Two Objects, One Class
Here is the part that confuses beginners. You can build many objects from the same class. Each one has its own separate state.
Phone jamilPhone = new Phone();
Phone ritaPhone = new Phone();
jamilPhone.model = "Galaxy A15";
ritaPhone.model = "iPhone 15";
jamilPhone.batteryLevel = 30;
ritaPhone.batteryLevel = 90;
jamilPhone and ritaPhone are two independent objects. Charging one does nothing to the other. Changing the model of one does not affect the other. They share the same blueprint, but they are separate houses on different plots of land.
This is the whole point of classes. You write the plan once, and you create as many objects as you need from it.
A Common Beginner Mistake
People sometimes write a class, then try to use its fields directly in main without creating an object first.
public class Phone {
String model;
public static void main(String[] args) {
model = "Galaxy A15"; // this will not compile
}
}
Java will refuse to compile this. The model field belongs to objects, not to the class itself. The class is the plan. The plan does not have a model. Only objects built from the plan have one.
You have two options. Either create an object first (like we did above), or mark the field as static, which makes it belong to the class itself. For now, stick with creating objects. static is a topic for another article.
Why This Matters
Once this idea clicks, the rest of Java gets easier. Every framework you will use later, from Spring to Android, is built on objects passing messages to other objects. The Java Language Specification devotes an entire chapter to classes because everything else builds on top of them.
If you are still shaky on the difference, try this exercise. Pick any object near you. A cup, a laptop, a rickshaw, a ceiling fan. Write down its state (a few fields) and its behavior (a couple of methods). Then translate that into a Java class. Do this five times with different objects. By the fifth one, classes and objects will feel obvious.
Quick Summary
- A class is a blueprint. It describes state (fields) and behavior (methods).
- An object is an instance built from a class. You create it with
new. - Each object has its own copy of the fields. Changing one object does not affect others.
- Use dot notation to read fields and call methods on an object.
- A field without an object makes no sense. Always create an object before using instance fields.
Based on dev.java/learn: Classes and Objects
Top comments (0)