From 1fba6f01270e13debfb4ecd5a48d5e5eb7b266d5 Mon Sep 17 00:00:00 2001 From: Tarun Jain Date: Wed, 4 Nov 2020 14:09:33 +0530 Subject: [PATCH] BAEL-4645|Transient keyword in Java --- .../java/com/baeldung/transientkw/Book.java | 42 ++++++++++++++++++ .../com/baeldung/transientkw/BookSerDe.java | 44 +++++++++++++++++++ .../transientkw/TransientUnitTest.java | 35 +++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/Book.java create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/BookSerDe.java create mode 100644 core-java-modules/core-java-lang-3/src/test/java/com/baeldung/transientkw/TransientUnitTest.java diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/Book.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/Book.java new file mode 100644 index 0000000000..5822d83841 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/Book.java @@ -0,0 +1,42 @@ +package com.baeldung.transientkw; + +import java.io.Serializable; + +public class Book implements Serializable { + + private static final long serialVersionUID = -2936687026040726549L; + + private String bookName; + private transient String description; + private transient int copies; + private final transient String bookCategory = "Fiction"; + + public String getBookName() { + return bookName; + } + + public void setBookName(String bookName) { + this.bookName = bookName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getCopies() { + return copies; + } + + public void setCopies(int copies) { + this.copies = copies; + } + + public String getBookCategory() { + return bookCategory; + } + +} diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/BookSerDe.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/BookSerDe.java new file mode 100644 index 0000000000..38e018baa3 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/BookSerDe.java @@ -0,0 +1,44 @@ +package com.baeldung.transientkw; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class BookSerDe { + static String fileName = "book.ser"; + + /** + * Method to serialize Book objects to the file + * @throws FileNotFoundException + */ + public static void serialize(Book book) throws Exception { + FileOutputStream file = new FileOutputStream(fileName); + ObjectOutputStream out = new ObjectOutputStream(file); + + out.writeObject(book); + + out.close(); + file.close(); + } + + /** + * Method to deserialize the person object + * @return book + * @throws IOException, ClassNotFoundException + */ + public static Book deserialize() throws Exception { + FileInputStream file = new FileInputStream(fileName); + ObjectInputStream in = new ObjectInputStream(file); + + Book book = (Book) in.readObject(); + + in.close(); + file.close(); + + return book; + } + +} diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/transientkw/TransientUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/transientkw/TransientUnitTest.java new file mode 100644 index 0000000000..cf63a0ddda --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/transientkw/TransientUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.transientkw; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class TransientUnitTest { + + @Test + void givenTransient_whenSerDe_thenVerifyValues() throws Exception { + Book book = new Book(); + book.setBookName("Java Reference"); + book.setDescription("will not be saved"); + book.setCopies(25); + + BookSerDe.serialize(book); + Book book2 = BookSerDe.deserialize(); + + assertEquals("Java Reference", book2.getBookName()); + assertNull(book2.getDescription()); + assertEquals(0, book2.getCopies()); + } + + @Test + void givenFinalTransient_whenSerDe_thenValuePersisted() throws Exception { + Book book = new Book(); + + BookSerDe.serialize(book); + Book book2 = BookSerDe.deserialize(); + + assertEquals("Fiction", book2.getBookCategory()); + } + +}