Solved

What is the best way to serialize multiple dissimilar objects

Posted on 2011-09-20
6
230 Views
Last Modified: 2012-05-12
I have figured out how to serialize and deserialize a single object including a collection of similar objects.  However, I'm unable to serialize/deserialize multiple dissimilar objects.  I thought about adding all the objects to a generic collection but that failed with compiler warnings and stack traces.

For starters, what is the best way for someone learning about making Java objects persistent, to make dissimilar objects persistent?  (Looking for an explanation of theory in English).

Second, can someone direct me to a code example dissimilar objects are persistent?  All I've been able to find are examples of saving a single object which included a collection of similar objects.  As I said, I don't need help with that.  I can do that.

0
Comment
Question by:Hugh McCurdy
  • 3
  • 3
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36569424

You can add them to raw collection
Just declare

ArrayList al = new ArrayList()

You'll see compiler warnings - but that is OK

Just make sure that all your objects are serializable
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569438
ArrayList does not need to have the same objects - and that is  the situation why
they allow to do it.

So don't waorry about these "unchecked" warnings - it will work just as well - as long as you know which of th elements id
of which type when you deserialize them
0
 
LVL 13

Author Comment

by:Hugh McCurdy
ID: 36569453
Hi for_yan, I thought you'd be the first one to post.  (Remember I called you a fast typer?)  I can't run a test now but I plan to tonight (when I'm home).  Thanks.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 47

Expert Comment

by:for_yan
ID: 36569494
Well, I just all the time mutilate English language...
I hope it will forgive me :)
After all over the history some people abused it even worse.

And don't worry - without generics all these collections work just fine.
It was an act of paternalism to create generics - they didn't believe people could be sufficiently organized to keep track of their types on their own.
0
 
LVL 13

Author Comment

by:Hugh McCurdy
ID: 36583645
This appears to work.  The only problem I have now is that I have an object (player) that has another object (room).  (The player is in a whatever room.)  After deserialization, the address of the room isn't the same as it was before serialization.  

Regardless, you have given me a useful answer.  I have an idea on how to solve the new problem.  If that doesn't work, I'll create a new question.  Thanks.
0
 
LVL 13

Author Closing Comment

by:Hugh McCurdy
ID: 36583672
The solution I used is slightly different but you provided valuable assistance.  Primarily, that it is normal to make a collection of serializable objects and cause them to be persistent.

I'm using this project as a self learning project to learn concepts not covered in the Java class I took.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now