If you reuse the same name over and over again for an object, are you creating a load of new ones or are you overwriting the original ones? For example : MyObject mo = null; mo = new MyObject(); mo = new MyObject(); mo = new MyObject();
Do I end up with three different objects here? - or one? Thanks. Paul
You created 3 objects but you are using only one reference. The objects no longer referenced will be eligible for garbage collection (which normally doesn't kick in until you are running low on memory) so you don't even have to worry about it. Junilu
I would add to this discussion, that the two objects you made first, while still 'there' (until eventual garbage collection), are no longer available, and there is no way to 'recover' a reference to these objects.
I think of it in terms of the classical programming problem of sorting. The simplest way is a bubble sort, and the crucial 'engine' is the swap function.
Because a computer needs a reference to an object (or value), there must be a reference for each and every object you want to 'hold on to'. So If you attempt the following: Our minds are capable of keeping track of things. When we want to swap things, we say to ourselves "put b where a was, and place a where b was". But a computer can't do that. We need a holding variable to keep track of the 'old' value for us. The same is true of object references. When you say mo = new Object(); Then mo is referring to an object instance. When you make the 'mo' reference point somewhere else, like making it point to another new Object()... that first instance is cut loose, and there is now no way to retrieve it.
Now if only computers could juggle.
[This message has been edited by Mike Curwen (edited June 02, 2001).]
What an education in juggling!! I had NO IDEA that there were so many PATTERNS. Now we just need a cute little applet like that to play with coding patterns .
Joined: Mar 15, 2000
So if I'm concerned about those other objects staying in existence I need to force garbage collection on them?
Originally posted by JUNILU LACAR: You created 3 objects but you are using only one [b]reference. The objects no longer referenced will be eligible for garbage collection (which normally doesn't kick in until you are running low on memory) so you don't even have to worry about it. Junilu[/B]
You can't force garbage collection, only suggest that it be run. GC depends on which JVM you're using. Isn't there some way you could just reduce the number of objects being created in the first place?