Fetching all the objects of a class

  • Thread starter Vincent Courcelle
  • Start date
V

Vincent Courcelle

Hello
i searching how to fetch all the objects of a class
what for ? those objects represents real objects dispatched in few rooms.
There is not a lot of objects, so when the player enter a room, i would like
to see each objects to view if they are in this room or not

another solution would be to put another class which manage those objects
(which would have a HashMap to remember where are each objects), but i
wouldn't like to do that (my project must be with the less class as
possible)

thanks !
 
P

Paul Lutus

Vincent said:
Hello
i searching how to fetch all the objects of a class
what for ? those objects represents real objects dispatched in few rooms.
There is not a lot of objects, so when the player enter a room, i would
like to see each objects to view if they are in this room or not

another solution would be to put another class which manage those objects
(which would have a HashMap to remember where are each objects), but i
wouldn't like to do that (my project must be with the less class as
possible)

Try phrasing your request this way:

1. I wanted this: ________________________________________________
2. But, darn it, I got this instead: _____________________________
3. Here is how (1) and (2) differ: _______________________________

Remember to ask for something clear and specific.
 
M

Michael Borgwardt

Vincent said:
i searching how to fetch all the objects of a class
what for ? those objects represents real objects dispatched in few rooms.
There is not a lot of objects, so when the player enter a room, i would like
to see each objects to view if they are in this room or not

another solution would be to put another class which manage those objects
(which would have a HashMap to remember where are each objects), but i
wouldn't like to do that (my project must be with the less class as
possible)

You don't need "another class" for it. Just make the HashMap (actually, you need
only a Set) part of the class itself, and have all objects register themselves
in it in the constructor.

However, this means that the objects can't be garbage collected, so you might
want to use a WeakHashMap.
 
V

Vincent Courcelle

Michael Borgwardt said:
You don't need "another class" for it. Just make the HashMap (actually,
you need
only a Set) part of the class itself, and have all objects register
themselves
in it in the constructor.

However, this means that the objects can't be garbage collected, so you
might
want to use a WeakHashMap.

Oh yes, i understand... and the HashMap will be inherited to the objects...
that seem to be great
something says me about "WeakReference", i will explore both
thanks a lot !
 
M

Michael Borgwardt

Vincent said:
Oh yes, i understand... and the HashMap will be inherited to the objects...

Not really, it should be a stati field of the class.
that seem to be great
something says me about "WeakReference", i will explore both

WekHashMap uses WeakReference for its keys.
 
A

Adam

Michael Borgwardt said:
You don't need "another class" for it. Just make the HashMap
(actually, you need
only a Set) part of the class itself, and have all objects register
themselves
in it in the constructor.

Make the collection (Set or any other more convenient
collection which would suit your needs)
a static member of the class.
That way a class can be a manager of its objects.
However, this means that the objects can't be garbage collected, so
you might
want to use a WeakHashMap.

Or have a non-static dispose() method in the class which
would unregister the object on which it is called from the collection.
But this would require careful programming, dispose() should be
probably called only if you are sure there are no more references
to that object. Any further operation on a disposed object should
be penalized by throwing some runtime exception.

Adam
 
M

Mark Haase

Vincent Courcelle said:
to see each objects to view if they are in this room or not

Why not just keep track of which objects are in each room?
wouldn't like to do that (my project must be with the less class as
possible)

Hmm, because its homework?
 

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

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top