What is Stateless Object in Java?

TU_HEO DAKAI picture TU_HEO DAKAI · Mar 16, 2012 · Viewed 47.3k times · Source

Currently I'm reading "Java concurrency in practice", which contains this sentence:

Since the action of a thread accessing a stateless object can't affect the correctness of operations on other threads, stateless objects are thread-safe.

So, what is stateless object?

Answer

Bozho picture Bozho · Mar 16, 2012

Stateless object is an instance of a class without instance fields (instance variables). The class may have fields, but they are compile-time constants (static final).

A very much related term is immutable. Immutable objects may have state, but it does not change when a method is invoked (method invocations do not assign new values to fields). These objects are also thread-safe.