Skip to content

Commit c344718

Browse files
author
Tarun Jain
committed
BAEL-4645|Transient keyword in Java
1 parent a8b8d2d commit c344718

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.transientkw;
2+
3+
import java.io.Serializable;
4+
5+
public class Book implements Serializable {
6+
7+
private static final long serialVersionUID = -2936687026040726549L;
8+
9+
private String bookName;
10+
private transient String description;
11+
private transient int copies;
12+
private final transient String bookCategory = "Fiction";
13+
14+
public String getBookName() {
15+
return bookName;
16+
}
17+
18+
public void setBookName(String bookName) {
19+
this.bookName = bookName;
20+
}
21+
22+
public String getDescription() {
23+
return description;
24+
}
25+
26+
public void setDescription(String description) {
27+
this.description = description;
28+
}
29+
30+
public int getCopies() {
31+
return copies;
32+
}
33+
34+
public void setCopies(int copies) {
35+
this.copies = copies;
36+
}
37+
38+
public String getBookCategory() {
39+
return bookCategory;
40+
}
41+
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.transientkw;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.ObjectInputStream;
8+
import java.io.ObjectOutputStream;
9+
10+
public class BookSerDe {
11+
static String fileName = "book.ser";
12+
13+
/**
14+
* Method to serialize Book objects to the file
15+
* @throws FileNotFoundException
16+
*/
17+
public static void serialize(Book book) throws Exception {
18+
FileOutputStream file = new FileOutputStream(fileName);
19+
ObjectOutputStream out = new ObjectOutputStream(file);
20+
21+
out.writeObject(book);
22+
23+
out.close();
24+
file.close();
25+
}
26+
27+
/**
28+
* Method to deserialize the person object
29+
* @return book
30+
* @throws IOException, ClassNotFoundException
31+
*/
32+
public static Book deserialize() throws Exception {
33+
FileInputStream file = new FileInputStream(fileName);
34+
ObjectInputStream in = new ObjectInputStream(file);
35+
36+
Book book = (Book) in.readObject();
37+
38+
in.close();
39+
file.close();
40+
41+
return book;
42+
}
43+
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.transientkw;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class TransientUnitTest {
9+
10+
@Test
11+
void givenTransient_whenSerDe_thenVerifyValues() throws Exception {
12+
Book book = new Book();
13+
book.setBookName("Java Reference");
14+
book.setDescription("will not be saved");
15+
book.setCopies(25);
16+
17+
BookSerDe.serialize(book);
18+
Book book2 = BookSerDe.deserialize();
19+
20+
assertEquals("Java Reference", book2.getBookName());
21+
assertNull(book2.getDescription());
22+
assertEquals(0, book2.getCopies());
23+
}
24+
25+
@Test
26+
void givenFinalTransient_whenSerDe_thenValuePersisted() throws Exception {
27+
Book book = new Book();
28+
29+
BookSerDe.serialize(book);
30+
Book book2 = BookSerDe.deserialize();
31+
32+
assertEquals("Fiction", book2.getBookCategory());
33+
}
34+
35+
}

0 commit comments

Comments
 (0)