EMZETT.
Login

Objects

In short: A specific instance of a class — while the class is only the blueprint, the object is the “thing” that actually exists in memory with its own, concrete values.

In more detail: Any number of objects can be created from the same class, each holding independent copies of the fields defined in the class (every car object, for example, has its own colour), but sharing the same methods. Access to an object happens via a reference (see Non-primitive Types), not via the direct value itself.

In Depth

The most important difference between an object and a primitive value (like a number) lies in HOW variables store and pass them on: a number variable contains the value directly, an object variable only contains a reference (basically a memory address) to the actual object, which sits elsewhere in memory.

Car car1 = new Car("Red");
Car car2 = car1;   // car2 points to the SAME object as car1!
 
car2.color = "Blue";
System.out.println(car1.color);   // prints "Blue" - both variables share the object

This behaviour surprises many beginners: car2 = car1 does NOT copy the car, only the reference to it — both variables then point to exactly the same object in memory. A change via one variable is therefore also visible via the other. If you actually want an independent copy, you have to explicitly request that (e.g. via a specially written “copy” method or a copy constructor).

An object exists in memory as long as something in the program still holds a reference to it — once the last reference loses its connection (e.g. because the variable leaves scope or is overwritten), the object counts as “no longer reachable” and, in languages with automatic memory management (e.g. Java, JavaScript, Python), is eventually removed from memory by the garbage collector, with no need for manual intervention.

Comparing two objects for content equality (do both have the same field values?) is different from checking whether two variables point to the SAME object (reference equality) — many languages deliberately offer two different comparison operations for this (e.g. .equals() vs. == in Java), and confusing the two is a common source of error.

See also: Classes, Non-primitive Types, OOP