Skip to main content

Week 2 Day 1 — Class versus Object

Goal

Today I want the OOP starting line: a class is a template, an object is an instance.

Main questions:

  1. What is a class?
  2. What is an object?
  3. What is the difference between instance and static members?
  4. What is null in this picture?
  5. How does this map onto Spring beans?

1. Class vs object

A class is the loaded template: fields, methods, constructors, nested types. The JVM keeps that metadata in Metaspace.

An object (instance) is a heap allocation built from that template.

public class User {
private final String name; // instance field: per object

public User(String name) {
this.name = name;
}

public String name() { // instance method: needs an object
return name;
}
}

User ada = new User("Ada"); // one object
User bob = new User("Bob"); // a second object

ada and bob share the class. They do not share the name field.

Memory sentence:

The class is the blueprint. new stamps out a heap object from that blueprint.


2. Members: instance vs static

KindBelongs toNeeds this?
Instance field / methodEach objectYes
static field / methodThe classNo
public class IdSequence {
private static long next = 1; // one counter for the class

public static long nextId() {
return next++;
}
}

static state is global to the class inside that JVM. It is shared by every instance and every thread that can see the class.

I use static for:

  • pure functions (Math.max, factories that do not remember anything)
  • constants (public static final String ROLE_ADMIN = "ADMIN")
  • true process-wide values I am willing to reason about as global

I do not use static for:

  • repositories, services, or anything I want to test with a fake
  • request or session data
  • “I need this in many methods” — that is an instance field or a parameter

A static counter on a Spring service is still global mutable state, just harder to see.


3. this and null

Inside an instance method, this is the reference to the current object.

public String name() {
return this.name; // explicit; often omitted
}

A reference variable can hold null: it points at no object.

User user = null;
user.name(); // NullPointerException: no instance to receive the call

NPE means: I used a reference as if it pointed at an instance. The class exists. The object does not.


4. One class, many objects, one bean

Class User (one, in Metaspace)
│ new
├── User@1 "Ada"
└── User@2 "Bob"

Class OrderService (one)
│ Spring creates once (singleton)
└── OrderService@1 ← every request stack points here

new User(...) in application code creates as many users as I ask.

@Service class OrderService is still a class. Spring typically creates one object and reuses it. The class can still be instantiated in a unit test with new OrderService(fakeRepo) — that is a different object, which is exactly why constructor injection is testable.

Memory sentence:

A Spring bean is an object. The annotation is on the class. The container decides how many objects.


5. Common traps

Trap 1: “The class is the object.”
The class is the type. Objects are instances.

Trap 2: Putting business state in static fields because “there is only one service.”
There is only one instance if Spring created one. Tests, extra contexts, and other threads still share static state even when they do not share the instance.

Trap 3: Calling instance methods as if they were static.
Without an instance there is no this and no instance fields.

Trap 4: User.class vs user.getClass().
.class is the compile-time class literal. getClass() is the runtime class of an object (which may be a proxy subclass).


Practice Questions and Answers

Question 1

What is the difference between a class and an object?

Answer:

The class is the definition loaded by the JVM: fields, methods, constructors. An object is a heap instance created from that class with new or by a container. Many objects can share one class.


Question 2

When is static the right tool?

Answer:

For code or data that belongs to the type, not to one instance: pure helpers, constants, factories without memory. Not for collaborators, request data, or anything I need to replace in a test.


Question 3

Why can I new OrderService(repo) in a test even if production uses @Service?

Answer:

@Service does not change the class. It is a hint to Spring to create and manage an instance. In a unit test I construct a separate instance myself and pass a fake repository. Two objects, one class.


Question 4

What does NullPointerException actually mean?

Answer:

I dereferenced a reference that did not point at an instance: called a method, read a field, or unboxed null. The class is loaded. There is no object at that variable.


Question 5

Is a singleton bean static?

Answer:

No. Singleton is a container scope: one instance, stored in the context, injected as a reference. The members are ordinary instance fields. static is a language rule that binds a member to the class regardless of Spring.


Memory sentences

The class is the blueprint. new stamps out a heap object.

static belongs to the type. Instance state belongs to the object.

A Spring bean is an object; the annotation is on the class.

Next: Week 2 Day 2 — Encapsulation and Access