Skip to content

Commit ea9bf9e

Browse files
timis1Iulian Timis
andauthored
JAVA-17420 Update The transient Keyword in Java Article (#13321)
Co-authored-by: Iulian Timis <iulian.timis@tora.com>
1 parent 0162d98 commit ea9bf9e

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

core-java-modules/core-java-lang-3/src/main/java/com/baeldung/transientkw/Book.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class Book implements Serializable {
1111
private transient int copies;
1212
private final transient String bookCategory = "Fiction";
1313

14+
private final transient String bookCategoryNewOperator = new String("Fiction with new Operator");
15+
1416
public String getBookName() {
1517
return bookName;
1618
}
@@ -39,4 +41,7 @@ public String getBookCategory() {
3941
return bookCategory;
4042
}
4143

44+
public String getBookCategoryNewOperator() {
45+
return bookCategoryNewOperator;
46+
}
4247
}

core-java-modules/core-java-lang-3/src/test/java/com/baeldung/transientkw/TransientUnitTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ void givenFinalTransient_whenSerDe_thenValuePersisted() throws Exception {
3434

3535
assertEquals("Fiction", book2.getBookCategory());
3636
}
37-
37+
38+
@Test
39+
void givenFinalTransientWithNewOperator_whenSerDe_thenValuePersisted() throws Exception {
40+
Book book = new Book();
41+
42+
BookSerDe.serialize(book);
43+
Book book2 = BookSerDe.deserialize();
44+
45+
assertNull(book2.getBookCategoryNewOperator());
46+
}
47+
3848
@AfterAll
3949
public static void cleanup() {
4050
File file = new File(BookSerDe.fileName);

0 commit comments

Comments
 (0)