user4685210
user4685210

Reputation:

I can't serialize and deserialize an object in Java

I have a problem while serializing an object in Java(8). I saw many examples, but none of them work for me. The thing is that while serializing it doesn't serialize the object with its complete data. When I try to deserialize, it reads all the variables as null. I do this with Employee class. The code for Serialize.java :

public class Serialize {
private static ArrayList<Employee> emp = new ArrayList<Employee>();
public static void main(String[] args){
    try{

            emp.add(new Employee("Areg Hovhannisyan",5));
            emp.add(new Employee("Tigran Hakobyan",15));
            emp.add(new Employee("Shivanshu Ojha",11));
            FileOutputStream fos = new FileOutputStream("emps.emp");
            ObjectOutputStream out = new ObjectOutputStream(fos);
            out.writeObject(emp);

            out.close();
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
}
}

Employee.java:

import java.io.Serializable;
public class Employee implements Serializable {
private static int age;
private static String name;

public static int getAge() {
    return age;
}
public static void setAge(int age) {
    Employee.age = age;
}
public static String getName() {
    return name;
}
public static void setName(String name) {
    Employee.name = name;
}


public Employee(String name,int i) {
    this.name = name;
    this.age = i;
}
@Override
public String toString() {
    return "Name : " + getName() + ", Age : " + getAge();
}

}

Please give an example to do this,how to deserialize and please give it with a explanation because I also want to understand how it works.

Upvotes: 3

Views: 4291

Answers (4)

Rajesh
Rajesh

Reputation: 384

As mentioned in the above comments, Statics are implicitly transient. Also as per your code if the variables are static, you'll have one vaule(which is finally added) in your Arraylist. This is the behaviour of static variable.

Please give an example to do this,how to deserialize

Code with Deserialization:

public static void main(String[] args) throws IOException,
        ClassNotFoundException {
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try {

        emp.add(new Employee("Areg Hovhannisyan", 5));
        emp.add(new Employee("Tigran Hakobyan", 15));
        emp.add(new Employee("Shivanshu Ojha", 11));
        fos = new FileOutputStream("emps.emp");
        out = new ObjectOutputStream(fos);
        out.writeObject(emp);

        fis = new FileInputStream("emps.emp");
        in = new ObjectInputStream(fis);
        ArrayList<Employee> empRead = (ArrayList) in.readObject();
        System.out.println(empRead.get(0));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.close();
        fos.close();
    }
}

Upvotes: 0

rns
rns

Reputation: 1091

Default serializable fields of a class are defined to be the non-transient and non-static fields.

Oracles Link

Here is stack OverFlow Link for it

Upvotes: 0

Vinze
Vinze

Reputation: 2539

The only problem in your code is that age and name fields should not be static for what you intend to do...

Your code should work just by removing the two static modifiers.

Then you maybe should read about static modifier to understand why your code couldn't work.

Upvotes: 0

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29266

This is because your fields in class are static. Statics are implicitly transient and we can not serialize transient fields.

Upvotes: 2

Related Questions