Solved

constructor while deserilizing object

Posted on 2016-08-05
16
54 Views
Last Modified: 2016-08-08
Hi,

Does constructor called while serializing object? please advise
0
Comment
Question by:gudii9
  • 6
  • 5
  • 5
16 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 41744605
Just like I said in your last question, try it.
1
 
LVL 16

Expert Comment

by:krakatoa
ID: 41744630
What gives you that idea may we know?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41744953
Looks no.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41746639
That's surprising. As I understand the Javadocs for Serializable, the first step of deserializing an object is to invoke whatever constructor it has that takes no parameters.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41746774
Which is not serialization of course.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41746791
Deserialisation I mean.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41747292
Now I've just noticed the title of this question says "deserializing" and the body says "serializing." The constructor is called during the former and not the latter.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41747414
Exactly my point.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 41747591
any good working example on this concept(not API link) of not calling constructor in deserialization and calling constructor on serialization?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41747593
O please.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41747595
Other way around. And no.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41747605
Got it other way around. Calls constructor in deserialization while from file to object (which is bit odd to me initially but since file to java object calling constructor makes more sense now) and calls no constructor while serializing object to file. I saw below example but no mention of constructor call though
http://www.tutorialspoint.com/java/java_serialization.htm
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41747609
Try that example, but add a constructor to the Employee class that prints something. You'll see it print something during deserialization.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41747612
sure will do that
0
 
LVL 7

Author Comment

by:gudii9
ID: 41747679
 import java.io.*;
public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

Open in new window


import java.io.*;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";
      e.SSN = 11122333;
      e.number = 101;
      
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

Open in new window


public class Employee implements java.io.Serializable
{
   public String name;
   public String address;
   public transient int SSN;
   public int number;
   
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + name + " " + address);
   }

public Employee() {

	System.out.println("for deserilize this constructor called");
}
   
}

Open in new window


when i run DeserializeDemo.java getting below error

java.io.FileNotFoundException: \employee.ser (The system cannot find the file specified)
      at java.io.FileInputStream.open0(Native Method)
      at java.io.FileInputStream.open(Unknown Source)
      at java.io.FileInputStream.<init>(Unknown Source)
      at java.io.FileInputStream.<init>(Unknown Source)
      at DeserializeDemo.main(DeserializeDemo.java:9)


i created employee.ser under root got similar error as above and moved to src still same error.

Please advise
serialized.png
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41747688
Did you run SerializeDemo first? It should create the file in a place that DeserializeDemo can read it. You could take the leading forward slash off of the filename, too (in both classes).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 45
egit plugin on eclipse 8 40
Tool Box 2 35
ASP.NET - Bind Gridview Datasource to a specific table in dataset 3 16
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

867 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