In a Java program which I did not made and cannot change, only write extensions for it, the design forces me to create an object which will never be garbage collected. I tested and I can do it through the use of statics or anonymous object instantiation. I know I can do something like:
public static MyObject eternalObject = new MyObject();
and have it accessible anytime I want on other parts of the program. But I can also make something like:
methodOfBadDesignedProgramWhichIDidNotCreate(new MyObject());
and the program will have the object available to use it.
I have analyzed the heap dump, and in both cases the object never gets garbage collected and lives until the end of the program. The program has a terrible design since it forces you to use a lot of statics for the creation of extensions. Not to mention that most of its methods are statics and has over 2000 static variables.
I've been trying to mitigate memory leaks on it and was wondering if one usage has any advantage over another. Note that contrary to popular belief, in this specific case, anonymous object instantiation does not die at the end of the method execution because the program is so badly designed that the reference is never lost. Even when the object is not in use.
So for an object that is never garbage collected by design, does anonymous object instantiation offer advantages over a static object creation?
P.S. I would understand if this question gets closed for being too localized as it is obvious this is very bad programming design that only a small amount of population would have to deal with so they would not care about it, but I have to deal with this bad design because my boss forces me to do so and doesn't allow me to create a new program from scratch, only create extensions for it. I am just trying to mitigate the memory leaks and do the best quality programming I can on the job. Therefore, I ask. To understand if one use has advantages over another and leave either educated or convinced that there is nothing I can do about this bad designed piece of software for which I cannot change the code or create a new one.