Skip to content

Commit 9b8b1e0

Browse files
committed
feat: implement serialization and deserialization with static and transient fields
WHAT: - Added `StudentFinal` class implementing `Serializable`. - Demonstrated behavior of `static` and `transient` fields during object serialization. - Implemented `SerilalizationOutputStreamFinal` to serialize a `StudentFinal` object to file. - Implemented `SerilalizationIutputStreamFinal` to deserialize and print object details. WHY: - To illustrate how Java handles object persistence using `ObjectOutputStream` and `ObjectInputStream`. - To show that: - `static` fields are not serialized (they belong to the class, not the instance). - `transient` fields are skipped during serialization for security or performance reasons. KEY POINTS: - `StudentFinal` contains: - `rollno`, `name`, `avg`, `dept` → serialized normally. - `Data` (static) → not persisted in file, retains default/class value after deserialization. - `t` (transient) → not serialized, resets to default value (`0` for int) upon deserialization. - Serialization: `SerilalizationOutputStreamFinal` writes object state to `Student3.txt`. - Deserialization: `SerilalizationIutputStreamFinal` reads object back and prints values. REAL-WORLD USE CASES: - Transient fields: used to skip sensitive info like passwords, security tokens, or cache references. - Static fields: excluded since they represent class-wide state, not per-object data. NOTES: - Always close streams after use to avoid resource leaks. - For consistency across JVM versions, explicitly define `serialVersionUID` in serializable classes. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 491cb8c commit 9b8b1e0

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Section23JavaIOStreams/Serialisation Storing Data in a File/src/SerilalizationIutputStreamFinal.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class SerilalizationIutputStreamFinal {
44
public static void main(String[] args) throws Exception {
5-
FileInputStream fis = new FileInputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Student3.txt");
5+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Student3.txt");
66
ObjectInputStream ois = new ObjectInputStream(fis);
77

88
StudentFinal s = ( StudentFinal ) ois.readObject();
@@ -11,4 +11,50 @@ public static void main(String[] args) throws Exception {
1111
fis.close();
1212
ois.close();
1313
}
14-
}
14+
}
15+
16+
/*
17+
1. Serialization kya hai?
18+
- Object → file/stream me binary form me convert karna.
19+
- Isse object ko disk pe save ya network ke through transfer kar sakte ho.
20+
- Java me `ObjectOutputStream` ka use hota hai.
21+
22+
2. Deserialization kya hai?
23+
- File/stream se binary form → original object banata hai.
24+
- Java me `ObjectInputStream` ka use hota hai.
25+
26+
3. Important keywords:
27+
- `implements Serializable` → bina iske class serialize nahi hogi (NotSerializableException aayega).
28+
- `transient` → jis variable pe lagao, wo serialize nahi hoga.
29+
- `static` → static variables class-level hote hain, object ke state ka part nahi bante, isliye ye bhi serialize nahi
30+
hote.
31+
32+
4. Code Explanation:
33+
- `SerilalizationOutputStreamFinal`:
34+
- FileOutputStream + ObjectOutputStream banaya.
35+
- StudentFinal ka ek object create kiya aur `oos.writeObject(s);` kiya.
36+
- File me pura object save ho gaya (binary form me).
37+
38+
- `SerilalizationIutputStreamFinal`:
39+
- FileInputStream + ObjectInputStream banaya.
40+
- `readObject()` karke wapas `StudentFinal` type cast kiya.
41+
- Print kiya → object state restore ho gayi.
42+
43+
5. Output Behaviour:
44+
- `rollno, name, avg, dept` → serialize + deserialize hote hain properly.
45+
- `static Data` → serialize nahi hota. Lekin constructor me last value set hoti hai, deserialization ke baad
46+
class-level value hi dikhegi (500).
47+
- `transient t` → serialize nahi hota, isliye deserialization ke baad iska value default ho jaata hai (0).
48+
49+
6. Best Practices:
50+
- Always use `serialVersionUID` in Serializable classes → version control ke liye.
51+
- Streams ko close karne ke liye try-with-resources use karo.
52+
- Agar sensitive data hai (password etc.), usko `transient` mark karo.
53+
54+
55+
✔ `Serializable` → required
56+
✔ `transient` → skip during serialization
57+
✔ `static` → not serialized (class-level)
58+
✔ `ObjectOutputStream.writeObject()` → save object
59+
✔ `ObjectInputStream.readObject()` → restore object
60+
*/

Section23JavaIOStreams/Serialisation Storing Data in a File/src/SerilalizationOutputStreamFinal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public String toString() {
3434

3535
public class SerilalizationOutputStreamFinal {
3636
public static void main(String[] args) throws Exception {
37-
FileOutputStream fos = new FileOutputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Student3.txt");
37+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Student3.txt");
3838
ObjectOutputStream oos = new ObjectOutputStream(fos);
3939

4040
StudentFinal s = new StudentFinal(10, "John", 89.9f, "CSE");
4141
oos.writeObject(s);
4242
fos.close();
4343
oos.close();
4444
}
45-
}
45+
}
97 Bytes
Binary file not shown.
109 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)