Copyright

Java: Transient Variables

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Transient variables in Java have a special power: their values aren't stored permanently. We'll be looking at these variables and serialization in this lesson, the latter being where transient variables are most used.

Serialization

In order to fully understand the concept of a transient variable, we'll have to learn what serialization is. Serialization is a fancy term to indicate that an object can be saved to a file. Java can take the bytes of the object (or a class) and serialize them, and save them sequentially in a file.

In our first example, we'll declare a Product class. As we prepare to serialize, let's add the necessary information right away. In order to serialize, you'll need to make sure to import the input/output utility. This is done in the import statement at the top of the code. Further, the class needs to be able to serialize, so we add code after the class statement. We implement the input/output tool called Serializable. It is part of the java.io package.

Take a look at the class definition below and notice how we use this utility. When we declare the class, we add the implements keyword so that we can use the java.io.Serializable class. This tells Java that you want your class to gain all the tools from this class. It will let us read and write files.


Java serializable class


Now, we can serialize the object. Depending on the developer tool, you can create a single project and have separate Java files within. One would be for the Product class and the other for the serialization.

In the following example, we again need to make sure that we're using Java's input/output utilities. The import statement at the top of the code brings in this utility: It lets us read and write files.


Java serialize code


Now let's read the file back and print out the output:


Java deserialize


When this runs, the output looks like:


Java output nontransient


We have successfully completed serialization and de-serialization. De-serialization is simply reading the file back into Java.

It makes sense to store some of the values to a file. But what about quantity on hand? That's the variable qoh. It may not make sense to store that value into a file, because it could change as the program runs. Therefore, we can make that variable transient.

Transient Variables

A transient variable is NOT serialized. In other words, it will not get saved into the file we create. Let's look at our Product class again, but this time we will declare the quantity on hand (qoh) as transient:


Java transient declaration


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support