Re: "shared" object




"Carsten H. Pedersen" <no@xxxxxxx> wrote in message
news:43a6cda0$0$67262$157c6196@xxxxxxxxxxxxxxxxxxxxxxxx
> Hello
>
> I want to create a kind of shared object. I have created a class, Counter,
> which you can then import. Through the Counter class, i would like to have
> access to a shared object.
>
> Lets say that Counter contains only an int, initialised to 1. Each time a
> class importing Counter would do Counter.getNextNumber(), the
> getNextNumber() method would increase the local int, and return the next
> number.
>
> What i want now is when an entirely different class, also importing
> Counter, calls the getNextNumber() method, it does not start out from 1,
> but keeps counting from where another class left off.
>
> I thought of maybe just writing the number to a file, and updating it each
> time getNextNumber() was called. Is there an easier way?
>
>
> - Carsten H. Pedersen

Stay away from static instances/variables, in spite of the earlier
responses. Although this is always a good idea the fact that you want
separate sequences implies that you should go for multiple insatnces of
sequence generators. If you could construct such a generator with an initial
sequence value (as opposed to 1) it would be easy to spawn sequences.

Silvio Bierman


.



Relevant Pages

  • Re: "shared" object
    ... >I want to create a kind of shared object. ... >Lets say that Counter contains only an int, ... >time a class importing Counter would do Counter.getNextNumber, ... >Counter, calls the getNextNumber() method, it does not start out from ...
    (comp.lang.java)
  • "shared" object
    ... i would like to have access to a shared object. ... Each time a class importing Counter would do Counter.getNextNumber(), the getNextNumbermethod would increase the local int, and return the next number. ... I thought of maybe just writing the number to a file, and updating it each time getNextNumber() was called. ...
    (comp.lang.java)