Objects
An object is a living instance of a class. It holds its own field values on the heap and answers method calls using that state.
Detailed Explanation
A class is a blueprint. An object is one built house. Payment describes what a payment looks like. new Payment("INV-9", 25000) creates one payment that exists until nothing refers to it any more.
You reach for an object when data and the rules on that data must travel together. A pair of loose variables named reference and amount can be passed in the wrong order, overwritten, or forgotten. An object packages them, names the type, and lets only the methods you wrote change the values.
Creation is explicit. new allocates space on the heap, writes default field values (0, false, null), runs field initializers, then runs a constructor. The expression yields a reference: a value you store in a variable that points at the heap object. Assigning that variable to another variable copies the reference, not the object. Two variables can name the same payment. Changing the amount through one name is visible through the other.
Identity and equality are different ideas. == on references asks "same object?". equals asks "same meaning?". Two String objects built from "hi" are equal and usually interned to the same instance, but two new Payment(...) objects with the same fields are different objects unless you write equals. Hash-based collections use both identity-sensitive defaults and value-sensitive overrides, so getting this wrong loses map entries.
Every object carries a header the JVM uses for class metadata, garbage collection, and locking. That is why a million tiny objects costs more than a million ints in an array. Objects also participate in the type system: a variable of type Payment can hold a Payment or a subtype, and the method that actually runs is chosen from the runtime class. That dispatch is polymorphism.
You do not always need an object. A static method on a utility class, a record for transparent data, or a primitive for a single number can be clearer. Create an object when the instance has a lifetime, unique state, or behaviour that callers should not reimplement.
The cost is allocation and later garbage collection, plus the mental cost of aliasing: any method that receives your object can mutate it unless the fields are final and the type is immutable. Prefer immutable objects when you can. When you cannot, document who owns the instance and who may change it.
Code Examples
public class Wallet {
private int balancePaise;
public Wallet(int balancePaise) {
this.balancePaise = balancePaise;
}
public void spend(int paise) {
balancePaise -= paise;
}
public int balancePaise() {
return balancePaise;
}
public static void main(String[] args) {
Wallet mine = new Wallet(10_000); // one object on the heap
Wallet alsoMine = mine; // second name, same object
alsoMine.spend(250);
System.out.println(mine.balancePaise()); // prints: 9750
System.out.println(mine == alsoMine); // prints: true
System.out.println(mine.equals(new Wallet(9750))); // prints: false (identity equals)
}
}public class Identity {
public static void main(String[] args) {
String a = new String("token");
String b = new String("token");
// Two objects, same characters. == is false; equals is true.
System.out.println(a == b); // prints: false
System.out.println(a.equals(b)); // prints: true
}
}Common Gotchas
Assignment copies the reference
`Wallet copy = original` does not clone. Mutating `copy` mutates `original`. Copy fields into a new instance, or use an immutable type, when you need independence.
Calling a method on `null` throws
a reference variable can hold `null`, meaning "no object". `wallet.spend(1)` then throws `NullPointerException`. Check, or refuse nulls at the constructor.
`==` is identity for objects
use `equals` for value comparison. For enums, identity and equality coincide, so `==` is acceptable.