View Single Post
  #12 (permalink)  
Old 06-28-2008, 03:24 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Java "for each" loops and Serializable objects.
Hello c26354
Quote:
Originally Posted by c26354
Code:
for (parameter : value) value = inputFromFile;
This example of a "for each" loop won't work properly. Consider this:
Code:
Vector<Dog> dogs = new Vector<Dog>(); populate(dogs); for (Dog dog : dogs){ dog = new Dog("Pete's clone"); }
The variable dog, carries a reference to an element of the vector dogs. So, if you assign a new dog to it, the original object or dog will remain unchanged. I always use "for each" loops when dealing with "read only" situations. With Java, what you are doing here can be dangerous.

Just a warning though. About your problem. The simplest way to do this is the make your objects implement the Serializable interface. Then you can use object streams to share the information, i.e. parameters and behavior. With good usage of Object Orientated concepts, you can make this a very dynamic system. This means that you do not have to use XML and extra API to store the information.

Good luck c26354 and ask if you would like to know more about object serialization.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote