BAEL-4645|Transient keyword in Java
This commit is contained in:
parent
5116a45d1e
commit
1fba6f0127
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue