What is an object in Java?
Solution and explanation
What it is
An object is an instance of a class: a real entity in memory with its own state (field values), behaviour (methods) and identity (its unique place in memory).
Example: a Laptop class defines brand, RAM and price. The Dell laptop on your desk with 16 GB RAM costing ₹65,000 is one object; your friend's HP is another.
public class Laptop {
String brand;
int ramGb;
double price;
void boot() { System.out.println(brand + " is starting with " + ramGb + " GB RAM"); }
}
Laptop mine = new Laptop();
mine.brand = "Dell"; mine.ramGb = 16; mine.price = 65000;
mine.boot(); // Dell is starting with 16 GB RAM
Three properties of every object
| Property | Meaning | In the example |
|---|---|---|
| State | Current values of its fields | brand = Dell, ramGb = 16 |
| Behaviour | What it can do | boot() |
| Identity | Distinguishes it from other objects even with equal state | its memory location; mine == other compares identity |
Every object inherits from Object
Methods available on every object: toString(), equals(), hashCode(), getClass(), wait()/notify()/notifyAll() and clone() (protected).
