flea whale
flea whale

Reputation: 1803

Can you iterate through every object of a particular class in Java?

Is there a way in Java to iterate through every object of a particular class, or do I have to keep track of each object as I construct/destroy with an array of references and access the object data like that?

Upvotes: 3

Views: 2072

Answers (8)

barry
barry

Reputation: 210

You could track all the live (and maybe a few extra) instances of a class by storing every new instance in a static WeakHashSet. But I'd have to agree that your design sounds suspect. From your more detailed description it sounds like the window needs a collection of elements (if it doesn't already) and it should be resppnsible for drawing them on screen.

Upvotes: 1

Borealid
Borealid

Reputation: 98509

The only way to get a list of all objects of a particular arbitrary class, without modifying any existing code, would be to explore the wonderful world of the Java garbage collector.

I don't think you want to do that, although it is theoretically possible. An easier way is to have the parent-class constructor keep track of each instance as it is created, registering it with some static handler.

But the best way is probably to use the factory pattern, where a "factory" class is responsible for both manufacturing and tracking instances of the class in question.

EDIT: note that you don't want to hold a strong reference to the object in such a class, or you'll prevent any objects' memory from being freed. A WeakReference will be necessary.

Upvotes: 1

blahman
blahman

Reputation: 1314

According to this SO question it doesn't really appear to be possible to effectively do it in Java. There are some rather heavy methods discussed here, and they're a little beyond me, but the poster in this question seems to ask something similar to what you're describing, and finally concludes in their edits that it doesn't appear to be easy to accomplish.

Upvotes: 0

devsnd
devsnd

Reputation: 7722

The only way you could do that, would be to keep a static List inside the class you want to keep an eye on and then to add each object to that list in the constructor.

You have to be aware, that the garbage collector then won't collect any of those objects anymore, even though you might not use them anymore.

Upvotes: 0

Jubal
Jubal

Reputation: 8717

There are libraries out there that give you the ability to navigate the object graph. Take a look at commons-ognl here: http://commons.apache.org/ognl/

Quote from the dev guide found here: http://commons.apache.org/ognl/developer-guide.html

"OGNL as a language allows for the navigation of Java objects through a concise syntax that allows for specifying, where possible, symmetrically settable and gettable values. The language specifies a syntax that attempts to provide as high a level of abstraction as possible for navigating object graphs; this usually means specifying paths through and to JavaBeans properties, collection indices, etc. rather than directly accessing property getters and setters (collectively know as accessors)."

It's been a while since I've used it, but at the time it was indispensable.

Upvotes: 0

beerbajay
beerbajay

Reputation: 20270

If there are no references to an object, they become candidates for garbage collection. So, yes, you have to keep track of them in some way. You can, but don't have to, use an array for this; there's also Java's collections.

Upvotes: 0

UmNyobe
UmNyobe

Reputation: 22910

I don't think there is a way. The problem is: when does an object is freed in java? The vm use something like a reference counter, and when it goes to 0 the object is not available anymore. So the vm itself cannot offer such a feature, as it require at least one reference on the object, or never doing garbage collect. As you create the object yourself, it is fairly easy to always keep track with a simple collection.

Upvotes: 1

Nicolas Modrzyk
Nicolas Modrzyk

Reputation: 14197

In Java, I would use aspects and pointcuts on initialization of a new object (constructor)

This instrumentation would not interfere with the normal run of the application and you could also turn this feature on or off as you want it.

Most of the current java tools support aspects.

Upvotes: 2

Related Questions