What is the nonserialized attribute in Java?

Java is one of the world's most well-known programming dialects for creating different programming applications. The nonserialized attribute is a crucial Java feature that indicates when a field or method cannot be serialized. This trait is usually utilized when certain fields or techniques ought not to be remembered for the serialization interaction, which changes Java objects into a stream of bytes.

Example

The keyword transientTransient in Java is used to indicate that a field should not be part of the serialization process. is used to apply a non-serialized attribute in Java, thus signaling that a field should not be serialized. When an object undergoes deserialization, the default value is assigned to the transient field. The default value assigned to a transient field during deserialization depends on the type of the field. For primitive types (such as int, float, boolean, etc.), the default values are as follows:

  • int: 0

  • float: 0.0

  • boolean: false

For object references (non-primitive types), the default value is null.

Take a look at the code example below:

import java.io.*;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
// transient keyword indicates that this field should not be serialized
private transient int age;
private transient String password;
public Person(String name, int age, String password) {
this.name = name;
this.age = age;
this.password = password;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPassword() {
return password;
}
}
class Main {
public static void main(String[] args) {
// Create a new Person object with name "John" and age 30
Person person = new Person("John", 30, "password123");
System.out.println("Before serialization:");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Password: " + person.getPassword());
// Serialize the person object to a file called "person.ser"
try {
FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in person.ser");
} catch (IOException e) {
e.printStackTrace();
}
// Deserialize the person object from the "person.ser" file
Person deserializedPerson = null;
try {
FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
deserializedPerson = (Person) in.readObject();
in.close();
fileIn.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Print the deserialized person's name and age
System.out.println("\nAfter deserialization:");
System.out.println("Name: " + deserializedPerson.getName());
System.out.println("Age: " + deserializedPerson.getAge());
System.out.println("Password: " + deserializedPerson.getPassword());
}
}

Explanation

In the code example sbove, we use the serializable interface, which shows how to serialize and deserialize a Java object.

  • Line 3: The code begins with the Person class, which implements the serializable interface to show that its objects may be serialized. There are three private fields in the class:

    • name

    • age

    • password

  • Lines 7–8: The transient keyword indicates that the age and password fields are not to be serialized. The serialVersionUID field is added to indicate the serialized object's version.

  • Lines 10–14: The Person class has a constructor that takes three arguments, which are used to initialize the private fields. There are three getter methods for accessing the private fields as well.

  • Lines 29–32: The Main class starts by creating a new Person object named "John." Before serialization, the values of the Person object are printed.

  • Lines 39–60: A file called person.ser is then created to store the serialized Person object. For this purpose, FileOutputStream and ObjectOutputStream are created. The output streams are closed when serialization is finished. After that, the code creates a FileInputStream and an ObjectInputStream to read the Person object from the person.ser file and deserialize it. The Person object is cast to the Person type after the ObjectInputStream is closed.

  • Lines 62–65: Finally, the code prints the deserialized Person object's values.

Note: Catch blocks are used to handle exceptions that may arise during serialization and deserialization. The stack trace is printed if an IOException occurs during serialization or if a ClassNotFoundException occurs during deserialization.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved