What is Stateless Object in Java?

前端 未结 10 1411
猫巷女王i
猫巷女王i 2020-11-30 21:32

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

Since the action of a thread accessing a stateless object can\'

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 22:01

    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.

提交回复
热议问题