I read about the temporary and final keyword, and I found the answer that we cannot use the transient keyword with the final keyword. I tried and got confused because here it works fine.
import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.Serializable; public class SerExample{ public static void main(String... args){ Student foo = new Student(3,2,"ABC"); Student koo = new Student(6,4,"DEF"); try { FileOutputStream fos = new FileOutputStream("abc.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(foo); oos.writeObject(koo); oos.close(); fos.close(); } catch(Exception e){} try{ FileInputStream fis = new FileInputStream("abc.txt"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.println(ois.readObject()); System.out.println(ois.readObject()); fis.close(); ois.close(); }catch(Exception e){} } }
Here is the Serializable Student Code class:
class Student implements Serializable{ private transient final int id; private transient static int marks; private String name; public Student(int id, int marks, String name){ this.id = id; this.marks = marks; this.name = name; } public Student(){ id=0; } @Override public String toString(){ return (this.name + this.id + this.marks); } }
Code output with a transient keyword.
ABC04 DEF04
Exit without a transient keyword.
ABC34 DEF64
Can you explain why it is working fine? is there an error
After all, what should be the behavior of the transition process with the final keyword?
java java-8 serialization final transient
user6117584 Jun 03 '16 at 12:11 2016-06-03 12:11
source share