I’d need to obtain all the objects created by a class. Is there any pre-built method
to do this in Java API?
I’d need to obtain all the objects created by a class. Is there any
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s not built into the API because there’s no such thing as “created by an object” in terms of Java. You could use a cache for this. But it’s not built into Java. You’d have to maintain this. You can save yourself some trouble by using Factory methods so you don’t forget to store it in the cache.
One thing to be concerned about is that this will cause a high potential for memory leaks. You could use WeakReferences to get around this, so that when every other thread loses access to the Object, so does the set. After all, you wouldn’t want your set to just store everything so it never gets GC’d.