Skip to main content

Serialization in Java for Beginners

Process of storing the state of object as it is in Java is called Serialization. The java.io.Serializable interface achieves Serialization in Java. The following tutorial explains Serialization in easy way. Let's kickstart.

Understand Better

Storing objects as it is means, storing the values of data member variables (global variables) as it is. Consider a simple class.
class emp
{
public int eno;
}
emp e=new emp();
e.eno=101;

Now, the value in eno sets to 101 after the statement e.eno=101 is executed. Now storing the value 101 in a file can be done by making class emp serializable using Serializable interface. The following example achieves this.

Real Time

If you can observe, software save user's settings i.e. when you check or un check a check box, that will be saved . How this could be possible? They either store it in the Registry or in a local file which may be of name settings.dat or something of their wish. So, in order to achieve this, Serialization is used. In fact, in my product Booster+ too, i've used the same technique.

java.io.Serializable interface

public interface java.io.Serializable {
}

An Example on Serialization

  1. Create Serializable class (should implement Serializable)
  2. Create & store it's object.
import java.io.*;
class Employee implements Serializable
{
public int eno;
public String ename;
public double sal;
}
class WriteSObject
{
public static void main(String args[]) throws Exception
{
Employee e=new Employee();
e.eno=1;
e.ename="Gowtham Gutha";
e.sal=200000.95;

FileOutputStream fout=new FileOutputStream("out.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);

out.writeObject(e);

out.close();
 fout.close();
}

}

Notes: 

1. FileOutputStream points out file where data has be written.

2. ObjectOutputStream points FileOutputStream.

Employee e=new Employee(): Create Employee class object.

e.eno=101;e.ename="Gowtham Gutha";e.sal=200000.95; 
Put values in data member variables of employee class.

out.writeObject(e): Write object 'e' in out.dat

out.close(): Close object output stream.

3. Now, read object & print using System.out.println().

import java.io.*;
class ReadSObject
{
public static void main(String args[]) throws Exception
{
FileInputStream fin=new FileInputStream("out.dat");
ObjectInputStream oin=new ObjectInputStream(fin);

Employee e=(Employee) oin.readObject();

System.out.println("Eno : "+e.eno);
System.out.println("Ename : "+e.ename);
System.out.println("Sal : "+e.sal);
}
}
Notes: 1. FileInputStream points out.dat (was previously written using FileOutputStream & ObjectOutputStream. See above)
2. ObjectInputStream points FileInputStream.

Employee e= (Employee) oin.readObject(): Read object from out.dat & store in e

3. Print them.

More: All wrapper classes (String, Integer, Double etc) are serializable.

Transient keyword

You know the meaning of transient? Well, it means pass away. So, who has to pass away? The variables, yes the variables that we don't need to be written into the file. How to do that? Simple, just place the transient keyword before the variable at declaration. That's it. Here is an example on transient keyword.

More examples on serialization

Here are two dead simple examples that could ever be written on Serialization. Unlike the above examples, they are simply about String objects.
  1. Read string from a file using ObjectInputStream
  2. Write string to a file using ObjectOutputStream

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.