• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Naming objects

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Pie
Posts: 7748
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
manish paliwal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, 3 different objects r created
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't name objects. You name variables that point to one object at a time. You made 3 objects. Your variable is pointing to the last one that you made.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Junilu Lacar
Bartender
Pie
Posts: 7748
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Curwen:
Now if only computers could juggle.

I know a Java Applet that does.

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic