I have been reading various articles about serialisation, I am a newb to Java, I have searched for my errors, but despite reading a lot, I fail to understand why my program won't compile and what I need to change. I've read that my problem is the arguments in my constructor, but nothing seems to fix it. I'm not looking for someone to do it for me, just a pointer in the right direction. I have these classes in seperate files and have tried with them in a package and out of a package.
import java.io.Serializable;
public class Employee implements Serializable
{
public String firstName;
public String lastName;
private static final long serialVersionUID = 5462223600l;
}
Employee.java has it's own file and it compiles on it's own
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializaitonClass { // held in SerializaitonClass.java
public static void main(String[] args) {
Employee emp = new Employee();
emp.firstName = "Vivekanand";
emp.lastName = "Gautam";
try {
FileOutputStream fileOut = new FileOutputStream("./employee.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(emp);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in ./employee.txt file");
} catch (IOException i) {
i.printStackTrace();
}
}
}
Serialisation.java does not compile, specific errors are: Constructor Employee in Class Employee cannot be applied to given types
. The compiler also doesn't recognise emp.firstName
and emp.lastName
Finally, can anyone suggest a very simple guide to serialization, I think I am close to understanding everything, but I need something which is written in "Newb" terms.