Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is the best way to serialize multiple dissimilar objects

Posted on 2011-09-20
6
Medium Priority
?
241 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:
Suggested Courses

824 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