instantiated objects of one class

At runtime, is there a way to get the references of all objects that are instances of one given class?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, the Class class doesn't define such a method, so I would think there isn't one. You can have a static Vector for your class and add each instance to it on construction and remove it on destruction (finalize()).
finalize() is not always guaranteed to be called,for threads at least.

Thread t=new Thread();
Thread [] currentThreads= new Thread [t.activeCount()];

there maybe a method to do it
A. AFAIK finalize() is guaranteed to be called, just the exact time is not guaranteed to be anything.
B. >> At runtime, is there a way to get the references of all
>> objects that are instances of one given class?
What's up with the thread count? The question was about instances of a class.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.


(A) AFAIK this is not the same even if all conditions are met in ALL versions of Java.

read this from Bruce Eckel's 'Thinking in Java'

When you run the program, you provide a command-line argument of “before” or “after.” The “before” argument will call the System.gc( ) method (to force execution of the garbage collector) along with the System.runFinalization( ) method to run the finalizers. These methods were available in Java 1.0, but the runFinalizersOnExit( ) method that is invoked by using the “after” argument is available only in Java 1.1[19] and beyond. (Note you can call this method any time during program execution, and the execution of the finalizers is independent of whether the garbage collector runs).

The preceding program shows that, in Java 1.1, the promise that finalizers will always be run holds true, but only if you explicitly force it to happen yourself. If you use an argument that isn’t “before” or “after” (such as “none”), then neither finalization process will occur, and you’ll get an output like this:

Created 47
Beginning to finalize after 8694 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 9834, total finalized = 108

Thus, not all finalizers get called by the time the program completes. [20] To force finalization to happen, you can call System.gc( ) followed by System.runFinalization( ). This will destroy all the objects that are no longer in use up to that point. The odd thing about this is that you call gc( ) before you call runFinalization( ), which seems to contradict the Sun documentation, which claims that finalizers are run first, and then the storage is released. However, if you call runFinalization( ) first, and then gc( ), the finalizers will not be executed.

One reason that Java 1.1 might default to skipping finalization for all objects is because it seems to be expensive. When you use either of the approaches that force garbage collection you might notice longer delays than you would without the extra finalization.


[19] Unfortunately, the implementations of the garbage collector in Java 1.0 would never call finalize( ) correctly. As a result, finalize( ) methods that were essential (such as those to close a file) often didn’t get called. The documentation claimed that all finalizers would be called at the exit of a program, even if the garbage collector hadn’t been run on those objects by the time the program terminated. This wasn’t true, so as a result you couldn’t reliably expect finalize( ) to be called for all objects. Effectively, finalize( ) was useless in Java 1.0.

[20] By the time you read this, some Java Virtual Machines may show different behavior.

(B) as for B I admit that the comment is a bt misleading ,what I wanted to say was this exactly(God what a difference a simple comma makes!)

finalize() is not always guaranteed to be called.

for threads at least.

            Thread t=new Thread();
            Thread [] currentThreads= new Thread [t.activeCount()];

            there maybe a method to do it

but  I dont know ,probably(note the comma now :-) ) you will have to dig deep into JVM related stuff,I do hope some enlightened soul would throw some light into this dark tunnel of ignorance.

to get back to ur question

public static int enumerate(Thread tarray[])

Copies into the specified array every active thread in this thread group and its subgroups.
This method simply calls the enumerate method of this thread's thread group with the array argument.

Cheers .....

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ok, I see what you mean now :-)
But I think that will only give you the active (running) Threads, not all instances of class Thread.
>>>>>At runtime, is there a way to get the references of all objects that are instances of one given class?

Yes Sasha_M I dont know how to get all other objects ,but I think for all threads at least you can keep track of thru this method.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.