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?
-
Are you making your own `Collection`? – Paul Feb 06 '12 at 01:21
-
1Why do you want do know this? It's an unusual thing to want to know, so answering that will help us answer your question. It sounds like your question is about a *way* to do something, so if you tell us what you are *actually* trying to do (in plain English) maybe we can offer some more elegant solutions. – Bohemian Feb 06 '12 at 01:27
-
@Bohemian I've only been learning Java for four days so be easy on me! I'm making a class called `Element` for elements displayed on the screen. They will be dynamically created so the number of objects made by this class is not predefined. Several other classes will extend this one and add a few methods. What I am thinking is that every time the window needs to be reprinted every element object can be looked at in turn and only if the visible property is set to `true` then it is drawn on the screen. – flea whale Feb 06 '12 at 01:38
-
@Jimmy_Bob What you usually do in that case is have a `registerElement(Element e)` method on the thing doing the drawing. – Borealid Feb 06 '12 at 01:42
-
Based on what you've told us, this is almost certainly not what you want to be doing. You should explicitly keep track of all the objects you're interested in; all the other options available to you are extremely complicated -- not something I'd recommend even if you'd been working in Java for two years. – Louis Wasserman Feb 06 '12 at 02:22
8 Answers
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.

- 13,961
- 2
- 36
- 40
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.

- 95,191
- 9
- 106
- 122
-
never heard of that before. Probably because I never need it.Still good to know. – UmNyobe Feb 06 '12 at 01:46
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.

- 22,539
- 9
- 61
- 90
-
I believe that the type of reference the garbage collector holds is called a *weak reference*; this enables the garbage collector itself to get at objects without stopping them from being collectable. – Borealid Feb 06 '12 at 01:35
-
Yeah you are right, but I am only talking about reference allowing the user code to access the object. – UmNyobe Feb 06 '12 at 01:41
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.

- 201
- 1
- 4
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.

- 19,652
- 6
- 58
- 75
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.

- 8,357
- 5
- 29
- 30
-
OGNL does not arbitrarily access objects outside of its execution context. OP is asking a different question. – Dave Newton Feb 06 '12 at 01:22
-
That's true, it probably doesn't help context wise (I like the aspectj answer for that and have voted it up.) – Jubal Feb 06 '12 at 01:30
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.

- 7,382
- 3
- 42
- 50
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.