Tag Archives: method

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

So we are going to learn that , how we doing serialization in Java.

Lets start...

i m going to create a class person named as Person which implements Serilizable interface.

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Person.java
package serialization;

/**
*
* @author Maria
*/
import java.io.Serializable;
public class Person implements Serializable {

private String firstname;
private String lastname;
person(String firstname,String lastname){

this.firstname = firstname;
this.lastname = lastname;

}

/**
* @return the firstName
*/
public String getFirstname() {
return firstname;
}

/**
* @param firstname the firstName to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}

/**
* @return the lastName
*/
public String getLastname() {
return lastname;
}

/**
* @param lastname the lastName to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}

}

In above class we have a setters and a getters of a serialiable class.

And now we have a main class in which we create the objects of Person class so we can use our serialized class.

Main.java 

package serialization;

/**
*
* @author  Maria
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {

public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{

FileInputStream fis;
FileOutputStream fos;
ObjectInputStream ois;
ObjectOutputStream oos;

String filename = "p.ser"; // creating a file named as p and .ser is extension for serlizable file
person p = new person("maria","soomro");

fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(p);

}
}

In Above Main.java class ,  the main methods throws some exceptions. ObjectOutputStream used for writing  object through writeObject  method.

When i run this main class the p.ser class is created

Serialization

Serialization

and for deserialization you have to write following code in calling class like in our example a Main class inside main method.

fis = new FileInputStream(filename);
ois = new ObjectInputStream(fis);
person pe = (person)ois.readObject();

System.out.println(pe.getFirstname());
System.out.println(pe.getLastname());
ObjectInputStream used for reading object through readObject method.

Output is :

Serialization

Serialization

 

 

 

 

 

 

 

 

5 Comments

I am going to start with a basic description for java variables because variables in any language are very useful works as a building block for any program , so lets start...

First of all What is meaning of word variable it means not having a fixed pattern. Its a container hold a value for u ,  When we define a variable with value at beginning of  any program after some code we can change  value of that variable , of course there are lots of other benefits of variables in java ....so we will describe later.

A variable is also called field so do not confuse . In java we can define  variables as :

1. Instance variables

2. Class variables

3. Local variables

1. Instance variables : A variables which also called non static field define with out static keyword and value of instance variable are unique for each instance of a class.

Note : you can not use instance variable in static method till you can not create a object of that class having instance variable.

2. Class variables : A variable with static modifier and value of a static field is same in all over code and we use it by class name do not need to create object.

3. Local variablesA variable only visible to the method where they are declared , no any use of special keyword with local variables.

How we can use these variables:

first we have to define datatype then name of variable end with semicolon like:

int varA ;

now you can assign value to variable here e.g: int varA = 10; or you can assign value where ever you want in code. The advantage of java variable are that we can define and declare a variable any where in the code unlike C.

Working with variables

Working with variables