Java transient keyword example
- Details
- Written by Nam Ha Minh
- Last Updated on 21 August 2019   |   Print Email
In Java, the transient keyword is used to mark a member variable (field) not a part of the persistent state of an object, i.e. the field will not be persisted when its parent object is serialized.
The following example shows a class which has a transient variable:
class Rectangle { int width; int height; transient long area; }
When an object of Rectangle class is being serialized, the fields width and height will be persisted, but the field area.
The transient keyword can be only applied for member variable, not for class, method and local variable.
See all keywords in Java.
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand how variables are passed in Java
- Understand encapsulation in Java
Comments