0
public class Test {

    private static int counter = 1;

    int uniqueId;

    public Test() {
        uniqueId = counter++;
    }

    public int getUniqueId() {
        return uniqueId;
    }
}


public class TestSub {
    public static void main(String args[]) {
        Test a = new Test();
        Test b = new Test();
    }
}
ernest_k
  • 44,416
  • 5
  • 53
  • 99
Naresh
  • 1
  • 3

1 Answers1

0

Assuming that:

  • the scope of int is large enough for your needs

  • you will not work with more than one thread at a time

  • you do not need randomness (for security reasons)

it should do exactly what you're describing.

Czarnowr
  • 83
  • 5