Serialization

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

I want to check a program about serialization.

I want to run program in this site:

http://webcache.googleusercontent.com/search?q=cache:uyLL01jf8rkJ:www.tutorialspoint.com/java/java_serialization.htm+http://www.tutorialspoint.com/java/java_serialization.htm&cd=1&hl=en&ct=clnk

But the error about the above code

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol : class SSN
location: class filejavaseria.Employee
at filejavaseria.Employee.<init>(Employee.java:12)
at filejavaseria.Main.main(Main.java:16)
Java Result: 1

public class Main {

    
    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();
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }




         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);
    }

}

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol : class SSN
location: class filejavaseria.Employee
at filejavaseria.Employee.<init>(Employee.java:12)
at filejavaseria.Main.main(Main.java:16)
Java Result: 1

I read that for a variable that you don’t want save, all I need is use transient.

But there is problem, Please guide me.

I have a concept about Serialization word, Is it right?
Stream consist of serial bytes, In serialization, an object became serial of bytes in stream. That’s why it called serialization.

There are a few problems with the code on that page, you’ve caught most of them.

For SSN, the modifier transient must appear before the type int.

Please check the attachment.This attachment, is from head first JAVA book.

FileOutputStream fileOut =
         new FileOutputStream("employee.ser");
         ObjectOutputStream out =new ObjectOutputStream(fileOut);
         out.writeObject(e);

When I look at the image in the book,It seems that at first, Object became serialized by ObjectOutputStream, and then wrtie on FileOutputStream.

But When I look at the code , I see at first fileOut stream is created, Then out is created.at then end out is written.

Is it good idea to say that fileOut is stream of bytes that filtered by ObjectOutputStream ?

Not really.

It’s a two step process.

FileOutputStream is an output stream that knows how to write bytes to a file, it knows nothing about how to serialize Objects.

ObjectOutputStream is an output stream that knows how to turn an Object into a stream of bytes (serialize), it knows nothing about writing to files.

So the code creates the file output, then creates an object output and tells the file output to expect input from the object output. It then feeds an object into the object output, which serializes the object and feeds it to the file output, which writes the bytes to a file.

In this way, we can have one object perform many tasks. For instance, the output from the ObjectOutputStream could be hooked up to a pipe, or even an onscreen display. Likewise, the FileOutputStream can take input from any of many sources and commit them to a file on a disk.

FileOutputStream is an output stream that knows how to write bytes to a file, it knows nothing about how to serialize Objects.

ObjectOutputStream is an output stream that knows how to turn an Object into a stream of bytes (serialize), it knows nothing about writing to files.

So the code creates the file output, then creates an object output and tells the file output to expect input from the object output.

Thanks. Very clear, Then that image is not true.

No…the image is correct.

1:  FileOutputStream fileOut =new FileOutputStream("employee.ser");
         2:  ObjectOutputStream out =new ObjectOutputStream(fileOut);
         3:  out.writeObject(e);

1: At first, an Output stream is created.

2: Then, An ObjectOutput stream is created

3: ObjectOutputstream convert object to serial bytes and write it on FileOutputStream. Now the object is serialized.

One question for me: streams are bytes that travel from program to file. FileOutputStream tranfers data by serial of bytes.But how bytes of ObjectOutputstream travels?

How ObjectOutputstream writes data on FileOutputStream ?

ObjectOutputStream determines what the object in question is, (Strings, for instance, are handled differently than ‘ordinary’ objects) and how to best go about creating a stream of bytes out of the object.

Once that’s all done, it simply writes the bytes to the OutputStream (in this case, a FileOutputStream) it was provided when it was constructed.

FileOutputStream fileOut =
         new FileOutputStream("employee.ser");
         ObjectOutputStream out =new ObjectOutputStream(fileOut);
         out.writeObject(e);

By using FileOutputStream, I create an Output stream, The name of this output stream is fileOut. Right?

Correct

Which shape do you prefer?