Jump to content

Stop an infinity loop

- - - - -

  • Please log in to reply
1 reply to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I need to create 20 objects of Class B, using while loop in class A. In class B i have a variable that should increment each time an object is created, how i should build class B's cunstractor so each time that variable increase and not become zero?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You'd use a static value, and increment that. Remember that this could cause problems with multithreading, so I suggest you use the AtomicInteger object instead of just a raw int.
import java.util.concurrent.atomic.AtomicInteger;


public class SomeClass

{

    private static AtomicInteger counter = new AtomicInteger();


    public SomeClass()

    {

        int objectNum = counter.getAndIncrement();

        // Do whatever it is you do.

    }

}

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users