I mean, I have a class A, and I have an array of A as a static data member of class A. Is is a bad practice?
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.
In Java, it will result in a memory leak … unless you do something messy and complicated with finalizers or
Referenceobjects.If your aim is to keep a collection of all instance ever created, you need to be very careful!! A collection of all instances created is going to leak memory, no matter how you implement it. (This is only really acceptable if the leak is either bounded, or small enough to not matter in the context of the entire application.)
If your aim is to keep a cache of existing instances to (for instance) to offset some particularly large object creation / initialization overheads, then you should use a
WeakHashMapor an existing 3rd-party cache class rather than trying to implement it from scratch using arrays.