aspose file tools*
The moose likes Beginning Java and the fly likes Naming objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Naming objects" Watch "Naming objects" New topic
Author

Naming objects

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
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

Joined: Feb 26, 2001
Posts: 5288
    
  10

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

Junilu - [How to Ask Questions] [How to Answer Questions]
manish paliwal
Greenhorn

Joined: May 18, 2001
Posts: 16
yes, 3 different objects r created

manish paliwal(or just mann )
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Feb 26, 2001
Posts: 5288
    
  10

Originally posted by Mike Curwen:
Now if only computers could juggle.

I know a Java Applet that does.

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Mar 15, 2000
Posts: 411
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

Joined: Feb 22, 2001
Posts: 156
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Naming objects