-2

I have a class, for example:

public class A{
    private final int number;

    public A(int number){
         this.number = number;
    }
}

Quesiton is, I want to update number time by time, and I must make A object stateless, which means nubmer has to be final. My friend suggested my to use AtomicInteger class, but I don't know how to make it work.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • `final` will make the property immutable. Statelessness depends on the context, i.e. what you're doing with `number` - you might perform an opterion with an `A` that doesn't get `A` in an distinguishable state from before the operation so that it'd be stateless in a sense - but again that depends on the sense which you need to define. – Kalle Richter Feb 29 '16 at 00:48
  • 2
    hmm.. how can A be stateless in any sense if it contains a mutable state? – ZhongYu Feb 29 '16 at 00:52
  • "I want to update number time by time, and I must make A object stateless" <- you cannot have both. This feels like an [X-Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem); what are you _really_ trying to do? What is the context and the problem you're solving? – Mick Mnemonic Feb 29 '16 at 01:00
  • Hi guys, I kind of figure out that what I need to do is turning `int` to `AtomicInteger`, in which way, it is more debugging friendly. – Jizhou Zhang Feb 29 '16 at 01:05
  • guy, thank for your reply, it is suggested using final AtomicInteger i, the reference never changes, but value in object could change. But I don't know whats the benefit for that. – Jizhou Zhang Feb 29 '16 at 01:39

2 Answers2

1

Having an AtomicInteger will not make the class stateless. Updating the integer, atomic or not, will change the internal state of the instance.

You could make the class immutable, i.e. state can't change, and create a new instance for each state change. Example:

public class A {
    private final int number;
    public A(int n) {
        number = n;
    }
    public int getNumber() {
        return number;
    }
    public A setNumber(int n) {
        return new A(n);
    }
}
Sason Ohanian
  • 795
  • 5
  • 16
  • guy, thank for your reply, it is suggested using final AtomicInteger i, the reference never changes, but value in object could change. But I don't know whats the benefit for that. – Jizhou Zhang Feb 29 '16 at 01:39
0

AtomicInteger are thread safe, you should use it like this :

public class Atomic{
  private AtomicInteger number;

  public Atomic(int number){
       this.number = new AtomicInteger(number);
  }

  public int getNumber() {
    return number.get();
  }

  public void setNumber(int number) {
    this.number.set(number);;
  }

}

But if you want to make something final you shouldn't use AtomicInteger, and final must be known at compile time so your solution is encapsulation, something like you did :

public final class Atomic{
  private int number;

  public Atomic(int number){
       this.number = number;
  }

  public int getNumber() {
    return this.number;
  }
}

For a stateless object I think you misunderstood what it means.

Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31