IT

Java Temporary Keywords: What Is It? How does it work?

Like all other programming languages, Java programming languages ​​have a list of reserved words that have a specific meaning. This set of 52 words reserved by Java is called a keyword. Programmers cannot use keywords to name classes, objects, variables, and other identifiers because they have predefined meanings. One of these 52 keywords is a temporary keyword. This is the focus of the blog.

Understand Java temporary keywords

Transient is one of the more useful modifiers that exist in Java programming languages. The main role of temporary keywords occurs in the case of serialization. Temporary modifiers can be applied to field members of a class to turn off serialization for these specific members.

Simply put, the Java transient keyword can be used by programmers to avoid serialization. If a particular object in a data structure is defined by the programmer as temporary, that object will not be serialized. As a result, you can use the transient keyword to indicate to the JVM (Java Virtual Machine) that a temporary variable is not part of the object’s persistent state.

Let’s use a very basic example to understand how the transient keyword works in Java. First, create the Address class and define the three attributes of the class (streetName, areaName, houseNumber). I don’t want to save or save the third attribute, houseNumber, so I’ll keep it temporarily.

Class Address implements Serializable

{{

Private string Road name;

Private string areaName;

Private temporary string houseNumber;

// Setters and getters

}

Then serialize an instance of the Address class.

try{{

ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (“addressinfo.ser”));

Address a = new Address ();

a.setstreetName (“MIG”);

a.setareaName (“Vikas Nagar”);

emp.sethouseNumber (“110”);

// Serialize the object

oos.writeObject (a);

oos.close ();

} catch (exception e)

{{

System.out.println (e);

}

If the code works correctly, the “houseNumber” information is defined as temporary and is not saved. Then deserialize it back to the original object and see if the information is stored.

try{{

ObjectInputStream o = new ObjectInputStream (new FileInputStream (“addressinfo.ser”));

// read back the object

Address readAddressInfo = (address) o.readObject ();

System.out.println (readAddressInfo.getstreetName ());

System.out.println (readAddressInfo.getareaName ());

System.out.println (readAddressInfo.gethouseNumber ());

o.close ();

} catch (exception e)

{{

System.out.println (e);

}

When I run the above Java program, I get the following output:

MIG

Bikas Nagar

null

As you can see, the “houseNumber” information was not saved in a permanent state due to serialization. Therefore, it receives the value “null” as output. This is why the transient keyword is used. This is to store the value in a variable in a temporary way rather than persistently.

Use case for the “transient” keyword in Java

Now that you understand the temporary keywords and their features, the next logical question to ask is when to use the temporary keywords.

Take a deeper look at some of the situations where using temporary keywords can benefit you and expand your learning about temporary keywords.

Here are some common use cases to keep in mind:

  • The first use case for a temporary keyword is when you have a data field that is calculated or derived from another field in an instance of the class. As a result, these fields need to be calculated programmatically and depend on other values. So instead of making such a field persistent, you can use the transient keyword to deserialize it. Examples of such temporary values ​​calculated from other numbers include timestamp-based values ​​and percentage-based values. In all of these cases, the value of the variable is calculated based on the value that currently exists.
  • Another important use case is when you are processing any type of secure information that should not be leaked outside the JVM and may not be serialized using the transient keyword.
  • Fields that are not marked as “serializable” in the Java Development Kit can be used with the transient keyword. The important thing to note here is that all Java classes that do not implement a serializable interface cannot be serialized because they are not referenced within the serializable class. As a result, they can throw a “java.io.NotSerializableException” if they are not marked “transient” before serializing the main class.
  • And finally, it may not make sense to serialize some fields. For example, if you add a logger reference to your code, you don’t need to serialize that information. You need to serialize the information that represents the state of your instance. Loggers, on the other hand, don’t represent state, so there’s no point in serializing them. Therefore, it should be used with the transient keyword.

The conclusion is

The above use cases are by no means exhaustive. Experiment with temporary keywords to come up with your own use case. After all, keywords are written for programmers, and most keywords can only be created if you understand the function and meaning of the keyword.

You’ve reached the end of this blog about Java’s temporary keywords. This is one of the more introductory concepts you need to learn to grow in your software development career. The field of software development is very dynamic, but if you are ready to take off your socks and do your best, you are very welcome.

upGrad has coached and coached students from different domains around the world to help them begin their careers in software development.our PGP in Software Development – ​​Full Stack Development Specialty Designed for beginners and experienced professionals. In addition to professional tutors and state-of-the-art learning materials, you’ll also receive one-on-one mentoring with talented peers, comprehensive placement support, and industry leaders. Please check the content of the course and register immediately.

1. What is the Java transient keyword?

You can use Java temporary keywords to avoid object serialization. Use temporary keywords to ensure that the values ​​held by a particular field are not persistent.

2. In what element of Java can the transient keyword be used?

You can use the transient keyword in Java to deserialize a class, variable, object, or field.

3. How are the transient and volatile keywords different in Java?

The transient keyword is used to avoid the serialization process, but it is not stored in main memory because the variable uses the volatile keyword.

Leading the AI-led technological revolution

https://www.upgrad.com/blog/transient-keyword-java/ Java Temporary Keywords: What Is It? How does it work?

Back to top button