-2

Will java.util.UUID work for classes in Java? That is to say i have a class that requires a unique id such that each time i construct an object of that class inside the main method, it will have a unique ID number.

the constructor might look something like this:

class flight{
        private UUID id;

        public void flight(){
           id = UUID.randomUUID();
        }
}

and the main method call might look something like this:

public static void main(String[] args){
    flight[] allflights = new flight[100];
    flight tempFlight;

    for(int i=0; i<100; i++){
        tempFlight = new flight()
        allflights[i] = tempFlight;
    }

Will this generate a unique ID for all the flights inside the flight array?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Bundy
  • 717
  • 3
  • 12
  • 23
  • possible duplicate of [How to generate unique ID in java (Integer)?](http://stackoverflow.com/questions/2178992/how-to-generate-unique-id-in-java-integer) – Björn Pollex Aug 22 '12 at 06:55
  • 1
    Not sure if you got a chance to look at the first response of http://stackoverflow.com/questions/325443/generate-uuid-in-java which talks about when a collision could happen! – Vikdor Aug 22 '12 at 07:00
  • 3
    Why would you want to do this? Can you not use an AtomicInteger as a counter? – Peter Lawrey Aug 22 '12 at 07:40
  • Take a look at [Flyweight design pattern](http://en.wikipedia.org/wiki/Flyweight_pattern). Looks like this is what you're after. – yegor256 Aug 22 '12 at 08:17

3 Answers3

3

As the documentation explains, this would indeed generate a unique id for each object:

Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

As Vidkor explains in a comment, there is a chance of collision though.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • Is it really true that it generates unique ids? I thought that UUIDs are *practically* unique rather than *guaranteed*. So it COULD be possible that two objects share the same id. All depends on the used algorithm, since it is a very long random generated number (Java it is 128bit I think). But its right to say: "Maybe, its never happen" :) – christian.vogel Aug 22 '12 at 07:02
  • Yes, I have already incorporated Vidkors comment, which links to a question dealing with this issue. – Björn Pollex Aug 22 '12 at 07:02
2

Yes, this is what UUIDs are for. You'll get a new random UUID every time you call randomUUID() ; a UUID is a 128bit value.

So theoretically you could get collisions when using random UUIDs, but as it says that a cryptographically strong random generator is used i don't think you need to bother with that possibility.

DThought
  • 1,340
  • 7
  • 18
-1

The answer to your question is in your question.

Run that code printing the UUIDs and you'll find out, what's so hard about adding System.out.println?

public static void main(String[] args){ flight[] allflights = new flight[100]; flight tempFlight;

for(int i=0; i<100; i++){
    tempFlight = new flight()
    allflights[i] = tempFlight;
    System.out.println(tempFlight.getId());
}
WhyNotHugo
  • 9,423
  • 6
  • 62
  • 70
  • This should be a comment, and even then, it is not a helpful one. Just observing a few UUIDs manually doesn't verify that all of them will be unique. – Björn Pollex Aug 22 '12 at 06:57
  • So the actual question is wether **Universally unique identifiers** are unique? – WhyNotHugo Aug 22 '12 at 06:58
  • No, the question is whether UUIDs generated like in the OPs code are unique. It may be a very simple question, but that doesn't mean one can't simply answer it. – Björn Pollex Aug 22 '12 at 07:00
  • I sincerely don't understand you. You're asking if unique ids are uniques? :/ – WhyNotHugo Aug 22 '12 at 10:04
  • 1
    As has been pointed out in comments by Vidkor and christian.vogel, UUIDs are in fact not guaranteed to be unique. However, the main reason that I criticized your answer is that it is unfriendly and unhelpful to the OP. It is not a good answer. – Björn Pollex Aug 22 '12 at 10:30