unique identifier for an object?

M

Markus Brosch

Hi java.programmer!

I was wondering if there is a method for objects, to get a unique (!)
identifier for the related object? Couldn't find anything yet :-/

Of course I can implement it by myself, but I tought the API offers such
an id?

Can anybody help? Thank you!
Best regards, Markus
 
A

Andrew Thompson

Markus said:
Hi java.programmer!

(Looks around) I am sure there were
other people here a moment ago!
I was wondering if there is a method for objects, to get a unique (!)
identifier for the related object? Couldn't find anything yet :-/

Does this do what you want?

public class IdentifyMyself
{
public static void main(String args[])
{
IdentifyMyself im = new IdentifyMyself();
System.out.println("Hello! I am " + im );
}
}
 
T

Thomas Weidenfeller

Markus said:
I was wondering if there is a method for objects, to get a unique (!)
identifier for the related object? Couldn't find anything yet :-/

Of course I can implement it by myself, but I tought the API offers such
an id?

What is it what you want to do? Because you already have one, the object
reference, which you keep in variables. You compare references with "=="
and "!=". Why is this not enough for your task?

/Thomas
 
M

Markus Brosch

What is it what you want to do? Because you already have one, the
object reference, which you keep in variables. You compare references
with "==" and "!=". Why is this not enough for your task?

I want to build something like an objectPool.
Why?

An indexer is indexing the content of my objects.
Later, I search for some keywords and as a result I need the object
itself again.

The index itself only accepts the following form:
Index.add(String key, String value)

My thought was, to build a map or tree with unique Object IDs (Strings)
which refer to the related Object?! In the index I store the ID as the
value.
Does this do what you want?

public class IdentifyMyself
{
public static void main(String args[])
{
IdentifyMyself im = new IdentifyMyself();
System.out.println("Hello! I am " + im );
}
}

More or less yes. But what, if my class has a toString. How can I access
that "ID" ?

Best regards, Markus
 
A

Andrew Thompson

Markus Brosch wrote:
(Thomas)
references > with "==" and "!=". Why is this not enough for your
task? ....
The index itself only accepts the following form:
Index.add(String key, String value)

After reading Thomas' response I realised
my approach was a bit ..sub-optimal.

Why do you not code another constructor
to your indexer that accepts Objects?

OTOH, you might also investigate..
hashCode()
[ or indeed, your Object constructor
might call it. ]
Read the JavaDocs carefully on this
one before you commit the future
of the company to it though..
 
M

Markus Brosch

After reading Thomas' response I realised
my approach was a bit ..sub-optimal.

Why do you not code another constructor
to your indexer that accepts Objects?

Hm, that indexer is org.apache.lucene.index, as I am using Lucene to
index and search my stuff - and it is optimized for text :-/

Your first approach:
As the API said: toString representation is default:
getClass().getName() + '@' + Integer.toHexString(hashCode())
(I got it!)
OTOH, you might also investigate..
hashCode()

HashCode itself is not unique. You mean I sould overwrite it right?

As a result of all this discussion:

////
WE STILL HAVE NO way to get an unique id, right???
////

Any further ideas?
Best regards, Markus
 
A

Andrew Thompson

Markus Brosch wrote:
....
HashCode itself is not unique. You mean I sould overwrite it right?

(shrugs vaguely) Hope I did not give you
the wrong impression, ...I was just kicking
a few ideas around.

Hopefully someone who has encountered
and solved this problem will happen along
any minute now..
 
P

Pat Ryan

how about using java.rmi.server.ObjID




Andrew Thompson said:
Markus Brosch wrote:
...

(shrugs vaguely) Hope I did not give you
the wrong impression, ...I was just kicking
a few ideas around.

Hopefully someone who has encountered
and solved this problem will happen along
any minute now..

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
 
M

Markus Brosch

Pat said:
how about using java.rmi.server.ObjID

I came along this class 5min ago ;-)
Better for my purpose is java.rmi.server.UID - only local uniqueness.
I'll give every object a UID an that's it mainly!

Thank you @all !!!
 
T

Thomas Weidenfeller

Markus said:
I want to build something like an objectPool.
Why?

An indexer is indexing the content of my objects.
Later, I search for some keywords and as a result I need the object
itself again.

The index itself only accepts the following form:
Index.add(String key, String value)

Then change it, take the existing collection classes like TreeMap or
HashMap as an example. You are not going to get a unique object ID via
toString() (subclasses override this to return all kinds of nonsens),
and you are not going to get a unique id via the hash code (different
objects are allowed to return the same hash). The only unique ID you
have is the object reference.


/Thomas
 
M

Markus Brosch

Markos said:
So what's wrong with hashCode()? Or is there not?

Don't get the point?
You mean I should use hashCode()?
As already mentioned: The default Hashcode ist NOT unique!

Best regards ;-)
 
C

Chris Smith

Jayaram said:
How about using -
System.identityHashCode(Object myObj)

Except that the result of System.identityHashCode is not guaranteed to
be unique. So it doesn't solve this problem in the slightest.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jayaram

How about using a combination of serialVersionUID and the output of
System.identityHashCode() (or a custom hashCode() implementation of
your class) ?
 
C

Chris Smith

Jayaram said:
How about using a combination of serialVersionUID and the output of
System.identityHashCode() (or a custom hashCode() implementation of
your class) ?

That doesn't solve anything. I don't really understand what you're
getting at. It's still not guaranteed to be unique.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
G

Gordon Beaton

I want to build something like an objectPool.
Why?

An indexer is indexing the content of my objects.
Later, I search for some keywords and as a result I need the object
itself again.

To me it doesn't sound like you need a globally unique ID, just one
that's unique among the objects you're pooling.

If these are "your own" objects, why don't you just number them
sequentially? Keep a counter variable in the class, and assign a
number to each object as you create it.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top