Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Remove a pointer
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zren




PostPosted: Sat Oct 09, 2010 1:33 pm   Post subject: Remove a pointer

Okay, say I want to remove a pointer to an object.

Eg. in a class called Town, I have a function called removeResidentFromTown(Resident resident) I have:
resident.town = null;

This is setup so that a Resident has a pointer to the town he is in. I want to reset the pointer instead of deleting the town. Which is what I think it's doing when I cast null onto it. So how would I go about setting the resident.town to null, but keep the Town instance that resident.town was referencing beforehand.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Sat Oct 09, 2010 3:06 pm   Post subject: RE:Remove a pointer

Java will destroy an instance when there are no longer any pointers pointing to it. When you do resident.town=null you are removing a pointer to it, if that was the last pointer, the town is destroyed.

This makes sense because when you remove the last pointer to an object, you have no way of ever accessing it again, so even if it still existed in memory, you can't do anything with it.

You just need to make sure you always have a reference to an object you will later need, maybe keep a list of all the towns?

---

Now that I think about it, I suspect this isn't the problem you are having. The null object will not act like a Town, even if it's in a variable that holds a Town. I *think* what you want to do is create a new town for the resident?

resident.town = new Town;

This will make sure that resident.town is always a town (and not null) (you will also need a constructor for Town that takes no arguments, or you will need to give it some arguments).

e; looking at your post a third time, I'm not sure if any of this is relevant to your problems, sorry if it isn't.
Zren




PostPosted: Sat Oct 09, 2010 3:40 pm   Post subject: RE:Remove a pointer

No dice. Thanks for trying though.

I'm trying to keep the current instance of the town. The town doesn't just refresh from new when a one person leaves does it? No, it should still exist in memory. The full structure is this:

World
World.HashMap<String, Town> towns
World.HashMap<String, Resident> residents

Town
Town.ArrayList<Resident> residents

Resident
Resident.Town town

So when I set resident.town to null, there should still be a reference to the town in the HashMap in World. Unless the garbage collector doesn't look for references between classes? or HashMaps?

Again the problem is that I don't know how to make resident.town not reference anything. I guess the proper words would be to go back before initializing it, without touching what it references.

A friend pointed out I could probably make a new Town, then load everything from the existing town minus that resident, but this seems overkill ya know. You normally do that when your going up in memory space, no when your losing it (and you not even changing any memory ta boot).
copthesaint




PostPosted: Sat Oct 09, 2010 10:41 pm   Post subject: RE:Remove a pointer

Cant you make a new pointer to a clone() of your town? Or am I thinking of something completely not what your talking about...
Zren




PostPosted: Sun Oct 10, 2010 6:00 am   Post subject: RE:Remove a pointer

Ah, seems I didn't get to the root of the problem. This wasn't the problem. Turns out my sorting algorithm to determine the capital (greating pop) was horrible. I learned about Comparables and Comparators today. Huzzah!

DtY was right where a nulling the second reference would not touch the first reference.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: