Understanding Classes and Objects in Java: A Drive Through the Basics
Published · Updated
A class defines a type’s state and behavior; an object is one particular
instance of that class created at runtime. If Car is the design, car1 and
car2 are separate cars, each with its own field values.
This updated example uses ordinary Java supported by current JDKs. It has no framework or external-library dependency.
Build the Car class
Save this as Car.java:
public class Car {
private final String model;
private final String color;
private final int year;
public Car(String model, String color, int year) {
this.model = model;
this.color = color;
this.year = year;
}
public String description() {
return year + " " + color + " " + model;
}
public void startEngine() {
System.out.println(model + " is starting its engine.");
}
}
There are four important pieces:
class Cardeclares a new reference type.- The three
private finalfields hold each object’s state.privatekeeps callers from changing them directly;finalmeans the constructor assigns them once. Car(...)is a constructor. It runs when code creates a newCar.description()andstartEngine()are instance methods. They operate on the object that receives the call.
Inside the constructor, this.model means “the model field on this object.”
The unqualified model is the constructor parameter. this removes the naming
ambiguity.
Create two objects
Save this as Main.java in the same directory:
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota Corolla", "red", 2020);
Car car2 = new Car("Honda Civic", "blue", 2019);
System.out.println(car1.description());
car1.startEngine();
System.out.println(car2.description());
car2.startEngine();
}
}
new Car(...) allocates a Car object and invokes its constructor. The variable
car1 holds a reference to the first object; car2 holds a reference to a
different object. Both objects share the method definitions from the class, but
their field values are independent.
Compile and run both files:
javac Car.java Main.java
java Main
Expected output:
2020 red Toyota Corolla
Toyota Corolla is starting its engine.
2019 blue Honda Civic
Honda Civic is starting its engine.
Class, object, and reference are different things
The distinction becomes clearer with assignment:
Car first = new Car("Toyota Corolla", "red", 2020);
Car alias = first;
This creates one Car object and two variables referring to it. Assignment
does not clone an object. By contrast, calling new Car(...) a second time
creates a second instance, even if all constructor arguments are identical.
Java passes object references by value. A method receives its own copy of the reference value, not a copy of the object. That detail matters later when you pass domain objects between controllers, services, and repositories.
Why keep fields private?
Private fields let a class protect its own rules. A mutable version of Car
might expose a method such as repaint(String newColor) that rejects a blank
color before changing the field. Callers ask the object to perform a valid
operation instead of editing its internals arbitrarily. This is encapsulation;
it is a design tool, not a guarantee that every class will be reusable or easy
to maintain.
Common beginner mistakes
- Naming the file differently from a
publicclass:public class Carbelongs inCar.java. - Forgetting
new:Car car;declares a variable but does not create an object. - Treating a reference as the object itself: multiple variables can point to the same instance.
- Making every field
public: this removes the class’s ability to control updates to its own state. - Putting both
public class Carandpublic class Mainin one source file.
Spring builds on these plain-Java ideas. A Spring bean is still an object; the difference is that the Spring container creates it and supplies its collaborators. Continue with IoC and dependency injection in Spring, or create your first Spring application.
Primary references
Object modelling and dependency boundaries are foundational to the applied backend work in the Forward-Deployed Engineer program.