What Is an Object in Java?

An object is a concrete instance of a class β€” a piece of memory that holds the data defined by the class and can perform the actions defined by its methods. You create objects with the new keyword.

Creating an object

// Class definition (the blueprint):
public class Car {
    String brand;
    int year;
    void honk() { System.out.println(brand + " beeps!"); }
}

// Object creation (the instance):
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year  = 2022;
myCar.honk();  // Toyota beeps!

myCar is a reference variable that points to a Car object on the heap. The object itself lives in memory; the variable holds an address to it.

Multiple independent objects

Car car1 = new Car();
car1.brand = "Toyota";

Car car2 = new Car();
car2.brand = "Honda";

car1.honk(); // Toyota beeps!
car2.honk(); // Honda beeps!

Each new Car() creates a separate object with its own copy of every field. Changing car1.brand does not affect car2.

Reference semantics

Car a = new Car();
a.brand = "BMW";

Car b = a;  // b now points to the SAME object as a
b.brand = "Audi";

System.out.println(a.brand); // Audi β€” same object!

Object variables hold references (addresses), not copies. Assignment copies the reference, not the object. To get an independent copy, you must implement a copy constructor or clone().

null

Car car = null;  // reference that points to nothing
car.honk();      // NullPointerException!

null means "no object". Calling a method on a null reference throws NullPointerException. Always check for null before using a reference that might be unset, or use Optional<T> to make nullability explicit.

java.lang.Object

Every class in Java implicitly extends java.lang.Object. This gives every object a set of methods by default:

  • toString() β€” string representation (override for readable output)
  • equals(Object o) β€” equality check (override for semantic equality)
  • hashCode() β€” hash for use in HashMaps and Sets (must be consistent with equals)
  • getClass() β€” returns the runtime class

Object creation on the heap

Java objects are always allocated on the heap (not the stack). The garbage collector reclaims heap memory when an object has no more references. You never call free() or delete in Java β€” memory management is automatic.