90

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?

10 Answers 10

127

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.

5
  • 26
    If an object has a field (non static), it has a state. If it is immutable, it has exactly one state, but is not stateless.
    – assylias
    Mar 16, 2012 at 10:41
  • 1
    @om-nom-nom 's answer touch much in-depth. May 9, 2016 at 10:17
  • @Bozho Why do not you post an example and give just a short explanation?
    – user19254373
    Jul 1, 2022 at 6:46
  • @om-nom-nom 's answer is much better yeah.
    – user19254373
    Jul 1, 2022 at 6:47
  • @Bozho Chico? Any reply please?
    – user19254373
    Jul 1, 2022 at 8:49
96

If the object doesn't have any instance fields, it it stateless. Also it can be stateless if it has some fields, but their values are known and don't change.

This is a stateless object:

class Stateless {
    void test() {
        System.out.println("Test!");
    }
}

This is also a stateless object:

class Stateless {
    //No static modifier because we're talking about the object itself
    final String TEST = "Test!";

    void test() {
        System.out.println(TEST);
    }
}

This object has state, so it is not stateless. However, it has its state set only once, and it doesn't change later, this type of objects is called immutable:

class Immutable {
    final String testString;

    Immutable(String testString) {
        this.testString = testString;
    }

    void test() {
        System.out.println(testString);
    }
}
5
  • 1
    "it has some fields, but it is known that they never change." That would be immutable, not stateless
    – assylias
    Mar 16, 2012 at 10:34
  • 1
    @assylias Not necessarily. If these fields are constants, and they are set at compile time, then we always know their values. This effectively means that the object doesn't carry any state information.
    – Malcolm
    Mar 16, 2012 at 10:37
  • 3
    Stateless objects have no state. Immutable objects with fields cannot transition to a different state from the one they were created in, so they have exactly one state.
    – assylias
    Mar 16, 2012 at 10:40
  • @assylias Yes, but I'm talking about the objects whose state is known before their creation. Not the ones that have some state set only once.
    – Malcolm
    Mar 16, 2012 at 10:42
  • While you were at it, you could also provide an example of a stateful object, however a lovely explanation.
    – knoxgon
    Apr 5, 2018 at 13:44
6

In simple terms state of object means value of internal variables in that object.

Stateful - state of object can be changed, means internal values off member variables of that object can be changed

How values can be changed?

By setting the value.

When can you set that value? When the variable is not final..

So, to make the class stateless, make the variable final, so that the value of that variable can't be changed neither in setter not in another method. It can be used only for computing.

5

The concept of stateless object is highly coupled with concept of side effects. Shortly, that is the object that has no fields underneath which could have different values, dependently on different order of method calls.

1
  • Good answer, voted up. But could you please post an example code to explain better?
    – user19254373
    Jul 1, 2022 at 6:48
2

An object without state, like instance variables that can change and vary depending on what has already happened to the object

2
Stateless: it has no fields and references no fields from other classes.

The state for a particular computation exists solely in local variables that are stored on the thread’s stack and are accessible only to the executing thread.

One thread accessing a method/class cannot influence the result of another thread accessing the same method/class; because the two threads do not share state, it is as if they were accessing different instances.

Since the actions of a thread accessing a stateless object cannot
affect the correctness of operations in other threads, Stateless objects are threadsafe.
1

A stateless object is an object that doesn't have any internal state (internal variable)

1

Just a clarification. You can consider your class as stateless in the way that is stated before, even when it has an instance variable as far as this variable is final AND immutable.

If the instance variable is just final but mutable, a List of Strings in example, yes the variable's reference can not be changed but the contents of the List and thus the state of the class can be changed.

1

If you can not change any parameter or value etc. of an object, after its creation, then that object is thread-safe.

0

An objects that have absolutely no state then there is no problem with reusing them at this point the question is: if they have absolutely no state why not make all the methods static and never create one at all?

1
  • “if they have absolutely no state why not make all the methods static and never create one at all?”----We should take polymorphism into account. Or just take into account OO's rule-- 'separating implementation from abstract' .It's is common in OO design that we have an Interface which represents an abstraction, while we can have several implementations----classes that implements our Interface. In this case, even if the implementing class is stateless, a class with only static methods does NOT work since it cannot implement our Interface. May 9, 2016 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.