Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Deserializing objects

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deserializing objects

    I have a problem about an objects deserialization: I serialized some objects different from one another but extend a X class. These objects are serialized in a random way and i don't know the correct order when they're about to be deserialized and I can't find a way to make a specific casting to their specific class. I thought to rescue each object and custing it to the X class, but then how do i make a down-casting? Using the reflection could be the solution?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deserializing objects

    For debugging you could use the getClass() method.
    Did you try using instanceof to test the object that was read?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deserializing objects

    I thought to use instanceof but there'd be 20 deserialized objects or more, so it wouldn't be "elegant" to make so many if-cases. Using the getClass() method how do I proceed? Class<?> c = objectDeserialized.getClass() and then c.newInstance()?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deserializing objects

    Using the getClass() method how do I proceed?
    Read the serialized object into an instance of the Object class and call getClass() on that object.
    Something like:
    Object obj = readObject()
    S.o.p(obj.getClass())

    so many if-cases.
    You will need code to handle each type of object that can be read.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deserializing objects

    Quote Originally Posted by Norm View Post
    Read the serialized object into an instance of the Object class and call getClass() on that object.
    Something like:
    Object obj = readObject()
    S.o.p(obj.getClass())
    Ok, but if I had to create that specific object as it was before the serialization?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deserializing objects

    You need a definition for the class that the jvm can use to build the object from the serialized object.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help on objects
    By alex067 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 10th, 2012, 06:23 PM
  2. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  3. Need help deserializing an integer array.
    By jeremy_ in forum Collections and Generics
    Replies: 10
    Last Post: May 30th, 2011, 11:50 AM
  4. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM