An architect draws a blueprint of a house. You can study it, measure it and argue about the colour of the kitchen, but you cannot sleep in it. Only when a builder follows the blueprint and puts up the walls does a real house exist, and the same blueprint can be used to build a second and a third house on other plots. Each house gets its own address, its own furniture and its own electricity bill.
In object-oriented programming the blueprint is a class and the house is an object. The Classes article showed how to write the blueprint. This article is about what happens after that: how objects are created, what they hold, how they are reached through variables, and how they work together. Every example uses Java and a small bank account story with two customers, Asha and Ravi.
1. What Is an Object?
An object is a real, usable instance of a class. It has an identity (which one it is), a state (the values stored in its fields) and a behaviour (what it can do through its methods). In Java you create an object with new and you reach it through a variable that refers to it.
A class is only a description. It takes no part in the running program until you create objects from it. A BankAccount class that is never instantiated holds no money and serves no customer.
One class, many objects: each object is built from the same blueprint but keeps its own values
The two ideas are easy to mix up, so here is the difference side by side:
| Class | Object |
|---|---|
| A blueprint or template | A real thing built from that blueprint |
| Written once in the source code | Created as many times as needed while the program runs |
| Describes which fields and methods exist | Holds actual values in those fields |
| Holds no field values of its own | Holds its own copy of every field value |
2. Identity, State and Behaviour
Every object has three things, and it helps to be able to name each of them when you read or design code.
The three characteristics of every object
- Identity is what makes this object different from every other one, even when their data looks the same. If Asha and Ravi both have Rs 1,000 in their accounts, there are still two accounts and the bank can tell them apart.
- State is the data the object carries at a given moment. For a bank account that is the holder name and the balance. State changes over time: a deposit changes the balance, and the account is the same account afterwards.
- Behaviour is what the object can do, written as methods. A bank account can accept a deposit, show its balance or transfer money.
A useful way to remember it: state is what the object has, behaviour is what the object does, and identity is which one it is.
3. Creating Objects in Java
Here is the whole idea in one runnable program. The class has two fields (the holder name and the balance), a constructor that sets them, and two methods. In main we create two objects from it.
Output
Asha: Rs 1000
Ravi: Rs 500
After Asha deposits Rs 500:
Asha: Rs 1500
Ravi: Rs 500
The word new does the work. It asks Java to reserve memory for a new object, then calls the constructor to fill in the starting values. The constructor has the same name as the class and no return type.
Look at the last two lines of the output. Asha made a deposit, and only Asha's balance changed. Ravi's object has its own copy of the state, so nothing that happens to one object leaks into the other. Here is the same program step by step:
| Step | Statement | What happens |
|---|---|---|
| 1 | new BankAccount("Asha", 1000) | Object 1 is created with balance 1000 and asha refers to it |
| 2 | new BankAccount("Ravi", 500) | Object 2 is created with balance 500 and ravi refers to it |
| 3 | asha.deposit(500) | Only object 1 changes: its balance becomes 1500 |
| 4 | ravi.show() | Object 2 is untouched and still shows 500 |
A small note on the code: the class BankAccount and the class Main sit in the same file, and only Main is public. Java allows one public class per file, and the file name must match it. Keeping the helper class without the public keyword is the easiest way to run a small example in a single file.
The Python and C++ tabs show the same program. Python creates the object by calling the class like a function, and C++ can create it directly as a local variable.
4. References: The Variable Is Not the Object
This is the part that confuses most beginners. In Java, a variable such as asha does not contain the account itself. It holds a reference to an object that lives elsewhere in memory, in an area called the heap. Think of the variable as a name tag or a remote control, and the object as the television it controls.
Reference variables point to objects. The variable and the object are two different things
That one idea explains a lot of behaviour that otherwise looks strange. Declaring a variable creates only the tag. The object appears when you write new. Until then the variable holds the special value null, which means "no object".
5. Two References, One Object
If a reference is just a tag, nothing stops you from sticking two tags on the same object. Assigning one reference variable to another copies the tag, not the object. The next program shows three variables and only two objects behind them.
Output
first balance: 1200
twin balance: 1000
first == alias: true
first == twin: false
spare points to no object yet
When we wrote alias = first we did not make a new account. We gave the existing account a second name. So alias.deposit(200) changed the one account that both names point to, and first saw the new balance of 1200.
twin was created with new, so it is a separate object that merely holds the same starting values. It never saw the deposit and still shows 1000.
first and alias are two names for one object; twin is a different object with the same values
This is also why two different comparisons exist:
==on two object references asks "do these point to the same object?". It checks identity, not content. That is whyfirst == twinprintedfalseeven though their data matched.equals()is the method a class can define to say when two objects count as equal in content. If you do not write it yourself, it falls back to the same check as==. Classes such asStringoverride it so that two strings with the same letters are equal.
The last lines of the program show a reference that points to nothing. If you call a method on a null reference, for example spare.deposit(100), Java throws a NullPointerException and the program stops. Checking if (spare != null) before you use a reference that might be empty is a habit worth building early.
In the C++ tab we used pointers to get the same "two names, one object" effect, because a plain C++ assignment between objects would copy the whole object.
6. Objects Working Together
A single object is rarely useful on its own. Real programs are made of objects that ask each other to do things. Let us let Asha transfer money to Ravi. The BankAccount gets a method that takes another BankAccount as a parameter.
Output
Transfer of Rs 300: true
Asha: Rs 700
Ravi: Rs 800
Transfer of Rs 2000: false
Asha: Rs 700
Ravi: Rs 800
Asha's account is responsible for checking that she has enough money and for subtracting it. It then asks Ravi's account to take care of its own side by calling deposit. Notice that it never reaches into Ravi's balance directly. Each object looks after its own state and only exposes methods for others to use. This is the idea of encapsulation in action.
The transfer: one object calls a method on another object
The second transfer asked for Rs 2,000, more than Asha had left, so the method returned false and neither balance changed. A good method refuses invalid requests instead of leaving the object in a half-changed state.
7. Passing Objects to Methods
When you pass an object to a method, what exactly does the method receive? Java always passes arguments by value. For an object, the value that gets copied is the reference. The method receives its own tag that points to the same object. The next program shows both sides of that rule.
Output
Asha: Rs 1100
Someone else: Rs 0
Asha: Rs 1100
Inside addBonus, the parameter and the caller's variable point to the same account, so the deposit is visible after the call. Inside replaceAccount, the assignment account = new BankAccount(...) only moves the method's own tag to a brand-new object. The caller's variable still points to Asha's account, which is why the last line is unchanged.
The method gets a copy of the reference, not the variable itself
So a method can change an object it was given, but it cannot make the caller's variable point to a different object.
8. Do I Need a New Object?
A quick question settles most design choices about when to write new and when to share a reference.
Create a new object only when it needs its own state
- If two things must be able to change independently, such as two customers' accounts, give each its own object.
- If two parts of the program are talking about the same real-world thing, such as the same account shown on two screens, pass the same reference around. Creating a copy would make the two screens drift apart.
9. Finding Objects in an LLD Problem
In a Low Level Design interview you rarely get a list of classes. You get a paragraph of requirements and must decide what the objects are. A reliable first pass is to underline the nouns, turn the important ones into classes, and then ask what real objects of each class will exist while the system runs.
Take an online mock-exam portal. The requirement says: "A student attempts an exam, answers questions and gets a score."
| Noun in the requirement | Class | An object at runtime |
|---|---|---|
| student | Student | Asha, who registered this morning |
| exam | Exam | The "Java Basics" mock test |
| question | Question | Question 7 of that test |
| attempt | Attempt | Asha's attempt at 7 PM, with her answers so far |
The table also shows why the class and the object must be kept apart in your head. There is one Student class but thousands of student objects, and each attempt is its own object with its own state. Once the objects are clear, the next step is to decide who is responsible for what, which is where the principles in the later articles help.
10. Common Mistakes
- Thinking each object gets its own copy of every method. Only the field values are per object. The method code is stored once for the class and shared by all objects.
- Assuming two objects with equal values are the same object. Equal data does not mean the same identity.
first == twinwasfalsein the example. - Using
==to compare content. It compares references. Useequals(), and override it in your own classes when content equality matters. - Forgetting that assignment copies the reference. After
b = athere is still one object. If you wanted an independent copy you must create one. - Calling a method on
null. A variable that was declared but never given an object holdsnull. - Believing a class creates objects by itself. A class does nothing until you write
new. - Making fields public so that other objects can reach in. Keep state private and expose behaviour through methods, as the transfer example did.
11. Try It Yourself
Write a Student class with a name and marks, a constructor that sets both, a method addMarks(int extra) that ignores negative values, and a method show(). In main, create two students, add marks to only one of them, and print both. Then add a third variable that is assigned from the first student and check that a change through it shows up in the first student too.
If your program prints the first student with the increased marks, the second student unchanged, and the first student again after the change through the third variable, you have understood identity, independent state and references.
Summary
- An object is a real instance of a class. The class is the blueprint and the object is the thing built from it.
- Every object has an identity, a state held in its fields and a behaviour defined by its methods.
- In Java you create an object with
new. Each object keeps its own state, so changing one does not affect another. - A variable holds a reference to an object, not the object itself. Assigning one reference to another gives the same object a second name.
==compares references,equals()compares content, and anullreference points to no object at all.- Objects work together by calling each other's methods, and a method that receives an object can change it but cannot change which object the caller's variable points to.
All the Java programs in this article were run on Java 21 and the outputs shown are the real outputs. The same code is available in Python and C++ in the code tabs.
Next, read about Enums, then see how objects are created, used and cleaned up in Object Lifecycle.